Aspirant Academy

MCQ

Paper - II (v) — Object Oriented Programming using C++ and JAVA MCQ - Practice Questions with Answers

Solve 45 Paper - II (v) — Object Oriented Programming using C++ and JAVA questions for RAS/RPSC preparation.

Practice questions

Q1Which statement about Java checked exceptions is correct?

A They must be either handled with a catch block or declared with throws
B They are subclasses of RuntimeException and need no compile-time handling
C They must always be caught inside the same method where they occur
D They can occur only during object construction
Explanation

Java distinguishes checked exceptions from unchecked exceptions. A method that may throw a checked exception must either handle it using an appropriate catch clause or declare it using a throws clause so the obligation is visible to callers. This rule is especially relevant in file handling, where input-output operations commonly declare checked exceptions.

Q2In C++ file handling, which class is most directly suited when the same file stream object must support both input and output operations?

A ifstream
B fstream
C ofstream
D stringstream
Explanation

The C++ stream library separates common file-stream roles: ifstream for input, ofstream for output, and fstream for file streams that can support both input and output depending on the open mode. A stringstream is also a stream, but it uses an in-memory string buffer rather than a file.

Q3In C++, a base class pointer points to a derived class object. Which declaration is essential if a call to the function through that pointer must use the derived class implementation at run time?

A Overload the function in the base class with a different parameter list.
B Declare the function as virtual in the base class and override it in the derived class.
C Declare the derived class object as static before assigning its address to the pointer.
D Make the base class data members public so that the pointer can inspect them.
Explanation

C++ run-time polymorphism through a base pointer or reference depends on virtual functions. If the base class declares a member function virtual and the derived class provides a matching override, the call is dispatched according to the dynamic type of the object. Changing storage duration, data-member access, or overload sets does not create dynamic binding.

Q4A C++ base class contains at least one pure virtual function. Which conclusion follows from this declaration?

A Objects of the base class can be created normally if every data member has a default value.
B The compiler converts the class into a Java interface at run time.
C The class becomes abstract and derived concrete classes must provide implementations for the pure virtual functions.
D All member functions of the class automatically become private.
Explanation

A pure virtual function expresses an abstract operation in a C++ class. The class cannot be directly instantiated, and a derived class that is meant to be concrete must provide final implementations for all inherited pure virtual functions. This is a core way to model interface-like behavior in C++ without changing access rules or involving Java interfaces.

Q5A C++ class Account has private data member balance. Which choice is the most appropriate object-oriented way to allow controlled modification of balance from outside the class?

A Make balance public so every function can assign to it directly.
B Store balance only in a local variable inside the constructor.
C Keep balance private and provide public member functions that validate and update it.
D Declare balance as global and let all Account objects share it.
Explanation

Encapsulation keeps an object's internal representation behind a public interface. For a field such as balance, the class should retain private storage and expose operations such as deposit or withdraw that validate amounts before changing the state. This is different from simply hiding data for its own sake; the point is to protect class invariants while still allowing useful operations.

You've seen 5 of 45 sample questions

Unlimited practice on Paper - II (v) — Object Oriented Programming using C++ and JAVA comes with the RAS Test Series + Practice pack or Gate Pass.

More questions

6Why is it usually important for a C++ base class intended for polymorphic deletion to have a virtual destructor?

AIt forces all derived classes to use private inheritance.
BIt ensures that deleting a derived object through a base-class pointer calls the proper destructor chain.
CIt prevents objects of the derived class from being constructed.
DIt makes every member function of the class static.

7A Java Swing button must execute code when it is clicked. Which approach best follows the usual event-handling model?

ACatch IOException around the button declaration because every click is delivered as a file error.
BOverride the main method so that it automatically receives every button event.
CContinuously poll the button in an infinite loop and check whether its label has changed.
DRegister an ActionListener with the button and place the response code in actionPerformed.

8Which statement correctly contrasts Java class inheritance with Java interface implementation?

AA class may extend several classes directly but implement only one interface
BInterfaces can contain only constructors and cannot declare methods
CA class may extend one direct superclass and may implement multiple interfaces
DA subclass cannot inherit any accessible members from its superclass

9In C++, which condition is necessary for a call through a base-class pointer to invoke the derived-class implementation at run time?

AThe base-class function is declared static
BThe object is allocated only on the stack
CThe base-class member function is declared virtual and is overridden in the derived class
DThe derived-class member function has a different name from the base-class function

10In Java, which statement correctly describes inheritance between classes?

AA class can extend more than one concrete class directly.
BPrivate fields of a superclass become directly accessible by name in every subclass.
CConstructors are inherited exactly like ordinary instance methods.
DA subclass can inherit accessible members of its superclass and may override inherited instance methods.

11Which C++ inheritance form is shown by the declaration class Printer : public Device { ... };?

APublic inheritance, where public base members remain public in the derived class interface.
BComposition, because Printer contains Device as a data member.
CFriend inheritance, where Printer becomes a friend of every Device object.
DMultiple inheritance, because every derived class has more than one base class.

12In Java file handling, why is try-with-resources preferred for objects such as FileInputStream or BufferedReader?

AIt makes the file contents immutable after reading
BIt automatically closes declared resources when the block finishes
CIt converts binary streams into character streams automatically
DIt guarantees that every IOException is ignored silently

13In Java event handling, what is the main role of a listener object?

AIt implements callback methods that are invoked when the registered event occurs
BIt replaces the event source so no component can generate events
CIt converts all run-time exceptions into checked exceptions
DIt permanently stores every event generated by the program

14In a C++ program, class Shape has a virtual member function draw(). Class Circle publicly derives from Shape and overrides draw(). If Shape* p points to a Circle object and p->draw() is executed, which statement best describes the call?

ABoth Shape::draw() and Circle::draw() are called automatically in base-to-derived order.
BThe call is illegal unless p is explicitly cast to Circle* before calling draw().
CCircle::draw() is called because virtual dispatch uses the dynamic type of the object through the base pointer.
DShape::draw() is always called because the declared type of p is Shape*.

15In Java, what does a finally block associated with try-catch primarily provide?

AIt declares a new subclass in the exception hierarchy.
BIt supplies code intended to run after try-catch processing, commonly for cleanup such as closing resources.
CIt converts every checked exception into an unchecked exception.
DIt must contain the only statement that can throw an exception.

More topics in Programming & Data Structures (Senior CI)

Explore other subjects