
This set examines polymorphism from both sides: compile-time overloading and run-time overriding. It covers virtual functions, vtables and vptrs, object slicing, virtual destructors, default arguments with virtual calls, overload resolution rules and how Java and C++ differ when virtual methods are called from constructors. It is aimed at GATE and university exam candidates and anyone preparing for C++ or Java interview rounds.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 18 questions.
Topics covered in this set
- Compile-time vs run-time polymorphism
- Function overloading and resolution rules
- Method overriding and covariant returns
- Virtual functions, vtable and vptr
- Object slicing and virtual destructors
- Static method hiding in Java
- override and final specifiers
Object Oriented Programming MCQs with answers and explanations
Q1. Which of the following is resolved at run time rather than at compile time?
a) Choosing between two overloaded functions with different parameter types
b) A call to an overridden virtual function through a base-class pointer
c) Selecting a template specialisation
d) Resolving an overloaded operator for built-in argument types
Q2. What is printed by this C++ program?
#include <iostream>
using namespace std;
struct B {
void f() { cout << "Bf"; }
virtual void g() { cout << "Bg"; }
};
struct D : B {
void f() { cout << "Df"; }
void g() { cout << "Dg"; }
};
int main() {
B* p = new D;
p->f();
p->g();
}
a) DfDg
b) BfDg
c) BfBg
d) DfBg
Q3. Using the same B and D as above (g virtual in B and overridden in D), what does this C++ code print?
B b = D(); b.g();
a) Bg
b) Dg
c) Compilation error
d) Undefined behaviour
Q4. What is printed by this C++ program?
#include <iostream>
using namespace std;
struct B {
virtual void f(int x = 1) { cout << "B" << x; }
};
struct D : B {
void f(int x = 2) override { cout << "D" << x; }
};
int main() {
B* p = new D;
p->f();
}
a) D2
b) B1
c) D1
d) B2
Q5. What is printed by this C++ program?
#include <iostream>
using namespace std;
void f(int) { cout << "int"; }
void f(double) { cout << "double"; }
int main() { f('a'); }
a) int
b) double
c) Compilation error: ambiguous call
d) Run-time error
Q6. What is printed by this C++ program?
#include <iostream>
using namespace std;
struct B { virtual ~B() { cout << "~B"; } };
struct D : B { ~D() { cout << "~D"; } };
int main() {
B* p = new D;
delete p;
}
a) ~B
b) ~D~B
c) ~B~D
d) ~D
Q7. What is printed by this C++ program?
#include <iostream>
using namespace std;
struct Base {
Base() { show(); }
virtual void show() { cout << "Base"; }
};
struct Der : Base {
void show() override { cout << "Der"; }
};
int main() { Der d; }
a) Der
b) BaseDer
c) Compilation error
d) Base
Q8. What does this Java program print?
class P {
P() { show(); }
void show() { System.out.print("P"); }
}
class Q extends P {
int v = 5;
Q() { show(); }
void show() { System.out.print(v); }
}
public class Main {
public static void main(String[] args) { new Q(); }
}
a) P5
b) 55
c) 05
d) PP
Q9. What does this Java program print?
public class Main {
static void m(Object o) { System.out.print("Object"); }
static void m(String s) { System.out.print("String"); }
public static void main(String[] args) { m(null); }
}
a) Object
b) Compilation error: ambiguous call
c) String
d) NullPointerException
Q10. What does this Java program print?
class P { static String s() { return "P"; } }
class Q extends P { static String s() { return "Q"; } }
public class Main {
public static void main(String[] args) {
P r = new Q();
System.out.print(r.s());
}
}
a) Q
b) PQ
c) Compilation error
d) P
Q11. What does this Java program print?
public class Main {
static void m(long x) { System.out.print("long"); }
static void m(Integer x) { System.out.print("Integer"); }
static void m(int... x) { System.out.print("varargs"); }
public static void main(String[] args) { m(5); }
}
a) Integer
b) varargs
c) long
d) Compilation error: ambiguous call
Q12. In a typical C++ implementation of virtual functions, which statement is true?
a) Each polymorphic class has one virtual table, and each object of such a class holds a hidden pointer to its class's table
b) Every object stores its own full copy of the virtual table
c) Virtual tables are created only for abstract classes
d) The virtual table stores the values of an object's data members
Q13. Which pair of C++ functions in the same scope is NOT a valid overload?
a) void put(int); and void put(double);
b) void put(int); and void put(int, int);
c) void put(int&); and void put(const int&);
d) int get(); and double get();
Q14. Which Java method can be overridden in a subclass?
a) A protected instance method
b) A private method
c) A static method
d) A final method
Q15. A Java superclass declares the method Animal create(). In subclass Dog (where Dog extends Animal), which declaration is a valid override?
a) void create()
b) Dog create()
c) Object create()
d) Animal create(int n)
Q16. Why is a virtual function call usually slightly more expensive than a non-virtual call?
a) Virtual functions are always interpreted rather than compiled
b) Each call copies the entire object onto the stack
c) The target is found through the object's vptr and vtable at run time, which adds an indirection and usually prevents inlining
d) Virtual functions must allocate heap memory on every call
Q17. What is the purpose of the C++11 override specifier on a member function?
a) It makes the compiler report an error if the function does not actually override a virtual function of a base class
b) It makes a non-virtual base function virtual
c) It prevents further overriding in derived classes
d) It forces the function to be inlined
Q18. A Java superclass declares public void run(). Which declaration of run() in a subclass compiles as a valid override?
a) void run()
b) protected void run()
c) private void run()
d) public final void run()
Quick revision notes
- Overloading is resolved at compile time; virtual overriding is resolved at run time.
- Dynamic dispatch needs a pointer or reference; passing by value slices the object.
- Default arguments come from the static type, even in a virtual call.
- Make the base destructor virtual if objects are deleted through base pointers.
- C++: virtual calls in a constructor use the class being constructed; Java dispatches to the subclass.
- Return type alone cannot distinguish overloads.
- Java static and private methods are not overridden; static ones are hidden.
- Java overload phases: widening, then boxing, then varargs.
Frequently asked questions
What is the difference between overloading and overriding?
Overloading means several functions with the same name but different parameter lists in the same scope, chosen at compile time. Overriding means a subclass provides its own version of an inherited virtual method with the same signature, chosen at run time based on the object.
What is a vtable in C++?
A vtable is a per-class table of function addresses for the class's virtual functions. Each object of a polymorphic class carries a hidden vptr to its class's vtable, and a virtual call jumps through that pointer.
Why should a base class have a virtual destructor?
If a derived object is deleted through a base pointer and the destructor is not virtual, the behaviour is undefined and usually the derived destructor never runs, leaking resources. A virtual destructor makes delete call the full destructor chain.
Comments
Post a Comment