
This set covers inheritance in depth: the five common types, the diamond problem and virtual base classes, how Java avoids multiple class inheritance, and the order in which constructors and destructors run. Several C++ and Java trace questions check field hiding and super calls. It is useful for university semester exams, GATE and UGC NET style questions and technical 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
- IS-A relationship and code reuse
- Single, multiple, multilevel, hierarchical and hybrid inheritance
- Diamond problem and virtual base classes
- Constructor and destructor call order
- super and this() in Java constructors
- Field hiding vs method overriding
- Composition vs inheritance
Object Oriented Programming MCQs with answers and explanations
Q1. Public inheritance should be used to model which relationship between classes?
a) IS-A, where the derived class is a specialised kind of the base
b) HAS-A, where one class contains another
c) USES-A, where one class calls another temporarily
d) PART-OF, where the lifetime of one depends on the other
Q2. What is printed by this C++ program?
#include <iostream>
using namespace std;
struct A { A() { cout << "A"; } ~A() { cout << "~A"; } };
struct B : A { B() { cout << "B"; } ~B() { cout << "~B"; } };
struct C : B { C() { cout << "C"; } ~C() { cout << "~C"; } };
int main() { C c; }
a) ABC~A~B~C
b) CBA~C~B~A
c) ABC~C~B~A
d) ABC~C
Q3. What is printed by this C++ program?
#include <iostream>
using namespace std;
struct X { X() { cout << "X"; } };
struct Y { Y() { cout << "Y"; } };
struct Z : Y, X {
Z() : X(), Y() { cout << "Z"; }
};
int main() { Z z; }
a) XYZ
b) YXZ
c) ZYX
d) Compilation error: initialiser list order mismatch
Q4. What is printed by this C++ program?
#include <iostream>
using namespace std;
struct A { A() { cout << "A"; } };
struct B : virtual A { B() { cout << "B"; } };
struct C : virtual A { C() { cout << "C"; } };
struct D : B, C { D() { cout << "D"; } };
int main() { D d; }
a) ABACD
b) ABCD
c) BCAD
d) ABCDA
Q5. What exactly is the diamond problem in multiple inheritance?
a) A derived class cannot have more than two base classes
b) Constructors of a class are called in an unpredictable order
c) A base class cannot be inherited privately
d) A class inherits from two classes that share a common base, so it may get two copies of that base and ambiguous member lookups
Q6. Which Java declaration will NOT compile?
a) class C extends A, B { }
b) class C extends A implements I, J { }
c) interface K extends I, J { }
d) class C implements I, J { }
Q7. A design has class Vehicle, classes Car and Truck derived from it, and class PickUp derived from both Car and Truck. What kind of inheritance is this as a whole?
a) Single
b) Multilevel
c) Hybrid
d) Hierarchical only
Q8. Classes Circle, Square and Triangle all derive directly from a single class Shape, and none derives from another. This is an example of:
a) Multiple inheritance
b) Multilevel inheritance
c) Hybrid inheritance
d) Hierarchical inheritance
Q9. What does this Java program print?
class G { G() { System.out.print("G"); } }
class H extends G {
H() { this(1); System.out.print("H"); }
H(int x) { System.out.print("h"); }
}
public class Main {
public static void main(String[] args) { new H(); }
}
a) HhG
b) hGH
c) GHhH
d) GhH
Q10. What does this Java program print?
class P { String n = "P"; String who() { return "P"; } }
class Q extends P { String n = "Q"; String who() { return "Q"; } }
public class Main {
public static void main(String[] args) {
P r = new Q();
System.out.print(r.n + r.who() + ((Q) r).n);
}
}
a) PQQ
b) QQQ
c) PPQ
d) QPQ
Q11. By default, which of the following is NOT inherited by a derived class in C++?
a) Public member functions of the base
b) Protected data members of the base
c) Friendship granted by the base class
d) Static data members of the base
Q12. In a C++ diamond (A as the base of B and C; D derives from B and C) using ordinary non-virtual inheritance, how many A subobjects does a D object contain?
a) 1
b) 2
c) 3
d) 0
Q13. What is the main reason behind the guideline 'favour composition over inheritance'?
a) Inheritance couples the subclass tightly to the base class implementation, so changes to the base can break subclasses
b) Composition is always faster at run time
c) Inheritance is not supported by most modern languages
d) Composition removes the need for interfaces
Q14. Where must an explicit super(...) call appear inside a Java constructor?
a) Anywhere, as long as it is called exactly once
b) As the last statement of the constructor
c) Inside a static initialiser block
d) As the first statement of the constructor body
Q15. What is the result of compiling and running this C++ program?
#include <iostream>
using namespace std;
struct Base { Base(int x) { cout << x; } };
struct Derived : Base {
Derived() { cout << "D"; }
};
int main() { Derived d; }
a) D
b) 0D
c) Compilation error
d) A garbage value followed by D
Q16. Classes B and C both define a public function show(), and D inherits publicly from both. For a D object d, how can C++ code call B's version unambiguously?
a) d.B::show();
b) d.show(B);
c) d->B.show();
d) B::d.show();
Q17. Which Java keyword, applied to a class, prevents any class from extending it?
a) static
b) abstract
c) private
d) final
Q18. Class D publicly inherits from class B in C++. Which conversion happens implicitly without a cast?
a) B* to D*
b) D* to B*
c) B object to D object
d) void* to D*
Quick revision notes
- Constructors run base to derived; destructors run derived to base.
- Multiple bases are constructed in declaration order, not initialiser-list order.
- Virtual base classes are constructed once, first, by the most derived class.
- Without virtual inheritance a diamond gives two copies of the top base.
- Java: one superclass per class, but any number of interfaces.
- super(...) or this(...) must be the first statement of a Java constructor.
- Fields are resolved by reference type; overridden methods by object type.
- Friendship and constructors are not inherited by default in C++.
Frequently asked questions
Why does Java not support multiple inheritance of classes?
Inheriting state and implementation from two classes causes ambiguities like the diamond problem. Java allows multiple inheritance of type through interfaces, and when two default methods clash the class must override and choose explicitly.
In what order are constructors called in multilevel inheritance?
From the topmost base class down to the most derived class, because each constructor first initialises its base part. Destructors run in the reverse order.
How does virtual inheritance solve the diamond problem in C++?
Declaring the intermediate classes with virtual public Base makes them share a single Base subobject. The most derived class becomes responsible for constructing that shared base, removing duplicate data and ambiguity.
Comments
Post a Comment