Inheritance Question Bank for C-CAT
Topic-wise Inheritance MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: C - Multiple
Multiple inheritance causes diamond problem when two parent classes inherit from same grandparent.
Show Answer & Explanation
Correct Answer: C - Virtual inheritance
Virtual inheritance ensures only one copy of base class members.
Show Answer & Explanation
Correct Answer: C - Public in derived
In public inheritance, public members remain public in derived class.
Show Answer & Explanation
Correct Answer: B - Private in derived
In private inheritance, public members become private in derived class.
Show Answer & Explanation
Correct Answer: D - Before derived constructor
Base class constructor is called first, then derived class constructor.
Show Answer & Explanation
Correct Answer: D - Derived first
Destructors are called in reverse order: derived first, then base.
Show Answer & Explanation
Correct Answer: A - Chain of inheritance (A->B->C)
Multilevel: A derives B, B derives C (chain).
Show Answer & Explanation
Correct Answer: D - One base, multiple derived
Hierarchical: multiple classes derive from one base class.
Show Answer & Explanation
Correct Answer: A - Inherited but not accessible
Private members are inherited but not directly accessible in derived class.
Show Answer & Explanation
Correct Answer: C - Protected
Protected members remain protected in public inheritance.
Show Answer & Explanation
Correct Answer: C - Before derived constructor
Base class constructor is always called first before the derived class constructor.
Show Answer & Explanation
Correct Answer: D - Derived first, then base
Destructors are called in reverse order: derived class destructor first, then base class.
Show Answer & Explanation
Correct Answer: B - Using member initializer list
In C++, base class constructor is called using member initializer list: Derived() : Base() {}.
Show Answer & Explanation
Correct Answer: A - Combination of two or more types
Hybrid inheritance combines multiple types of inheritance like hierarchical and multiple.
Show Answer & Explanation
Correct Answer: A - Protected
In protected inheritance, public members of base class become protected in derived class.
Show Answer & Explanation
Correct Answer: C - Prevents inheritance
A class declared as final cannot be inherited by any other class.
Show Answer & Explanation
Correct Answer: D - Both base and derived objects
Base class pointer can point to both base and derived class objects (upcasting).
Show Answer & Explanation
Correct Answer: B - Constructors
Constructors and destructors are not inherited; derived class must define its own.
Show Answer & Explanation
Correct Answer: B - Changes access of inherited member
using Base::member can change the access level of inherited members in derived class.
Show Answer & Explanation
Correct Answer: A - Base members + derived members
Size of derived class includes size of base class members plus derived class members.
Which type of inheritance is shown below?
class A {};
class B : public A {};
class C : public B {};Show Answer & Explanation
Correct Answer: B - Multilevel inheritance
This is multilevel inheritance where class C inherits from B, and B inherits from A, forming a chain of inheritance.
Show Answer & Explanation
Correct Answer: C - private
When no access specifier is specified in class inheritance, the default is private. For structs, the default is public.
Show Answer & Explanation
Correct Answer: D - Public and protected members
With public inheritance, public and protected members of the base class are accessible in the derived class. Private members are inherited but not directly accessible.
Show Answer & Explanation
Correct Answer: B - When a class inherits from two classes that share a common base class
The diamond problem occurs when a class inherits from two classes that both inherit from a common base class, creating ambiguity about which copy of the base class members to use.
Show Answer & Explanation
Correct Answer: B - Using virtual base classes
Virtual base classes resolve the diamond problem by ensuring that only one copy of the base class exists in the derived class, even when inherited through multiple paths.
What will be the output?
class Base {
public:
Base() { cout << "Base "; }
};
class Derived : public Base {
public:
Derived() { cout << "Derived "; }
};
int main() {
Derived d;
}Show Answer & Explanation
Correct Answer: C - Base Derived
When a derived class object is created, the base class constructor is called first, followed by the derived class constructor. Output: 'Base Derived'.
Show Answer & Explanation
Correct Answer: B - Derived class first, then base
Destructors are called in the reverse order of construction. The derived class destructor is called first, followed by the base class destructor.
Show Answer & Explanation
Correct Answer: D - class C : public A, public B {}
Multiple inheritance in C++ uses comma-separated base classes with their access specifiers: class C : public A, public B {}.
What will be the output?
class A {
public:
void show() { cout << "A"; }
};
class B : public A {
public:
void show() { cout << "B"; }
};
int main() {
B obj;
obj.show();
}Show Answer & Explanation
Correct Answer: B - B
The derived class B has its own show() function which hides the base class version. When called on a B object, the derived class version is invoked, printing 'B'.
Show Answer & Explanation
Correct Answer: A - A::show()
In C++, the scope resolution operator is used to call the base class version: A::show(). C++ does not have super or parent keywords.
Show Answer & Explanation
Correct Answer: A - They are inherited but not accessible in derived class
Private members of the base class are inherited by the derived class but are not directly accessible. They can only be accessed through public or protected member functions of the base class.
What will be the output?
class A {
public:
A() { cout << "A"; }
};
class B {
public:
B() { cout << "B"; }
};
class C : public A, public B {
public:
C() { cout << "C"; }
};
int main() {
C obj;
}Show Answer & Explanation
Correct Answer: B - ABC
In multiple inheritance, base class constructors are called in the order they are listed in the inheritance declaration. A is listed first, then B, then C's own constructor. Output: 'ABC'.
Show Answer & Explanation
Correct Answer: B - Public members of base become protected in derived
With protected inheritance, public members of the base class become protected in the derived class, and protected members remain protected. Private members stay inaccessible.
Show Answer & Explanation
Correct Answer: D - Distributed
Distributed is not a type of inheritance in C++. The valid types are: single, multiple, multilevel, hierarchical, and hybrid inheritance.
Show Answer & Explanation
Correct Answer: C - Multiple classes inherit from one base class
Hierarchical inheritance is when multiple derived classes inherit from a single base class. For example, classes Dog, Cat, and Bird all inheriting from class Animal.
What will be the output?
class Base {
protected:
int x = 10;
};
class Derived : public Base {
public:
void show() { cout << x; }
};
int main() {
Derived d;
d.show();
}Show Answer & Explanation
Correct Answer: A - 10
Protected members of the base class are accessible in the derived class. The derived class can access x directly, so it prints 10.
Show Answer & Explanation
Correct Answer: D - Both A and B are correct
Both 'virtual public' and 'public virtual' are valid syntaxes for declaring a virtual base class in C++. The order of virtual and access specifier doesn't matter.
What will be the output?
class A {
public:
A(int x) { cout << x; }
};
class B : public A {
public:
B(int x) : A(x * 2) { cout << x; }
};
int main() {
B b(5);
}Show Answer & Explanation
Correct Answer: C - 105
B's constructor passes x*2 = 10 to A's constructor, which prints 10 first. Then B's constructor body prints 5. Output: '105'.
Show Answer & Explanation
Correct Answer: A - No, it must use the base class constructor
A derived class cannot directly access or initialize private members of the base class. It must use the base class constructor through the initializer list.
Show Answer & Explanation
Correct Answer: D - Combination of two or more types of inheritance
Hybrid inheritance is a combination of two or more types of inheritance (e.g., combining multilevel and multiple inheritance). It can lead to the diamond problem.
What will be the output?
class A {
public:
void display() { cout << "A "; }
};
class B : public A {};
class C : public A {};
class D : public B, public C {};
int main() {
D d;
d.display();
}Show Answer & Explanation
Correct Answer: A - Compilation error (ambiguity)
This is the diamond problem. Class D inherits display() from both B and C (which both inherit from A), creating ambiguity. The compiler cannot determine which version to call.
Show Answer & Explanation
Correct Answer: A - Using scope resolution: d.B::display()
Without virtual inheritance, the ambiguity can be resolved using the scope resolution operator to specify which path to use: d.B::display() or d.C::display().
Show Answer & Explanation
Correct Answer: B - Public and protected become private
In private inheritance, both public and protected members of the base class become private in the derived class. They are not accessible outside the derived class.
Show Answer & Explanation
Correct Answer: A - final
The 'final' keyword (introduced in C++11) prevents a class from being inherited. Example: class A final {}; Any attempt to inherit from A will cause a compilation error.
What will be the output?
class A {
public:
~A() { cout << "~A "; }
};
class B : public A {
public:
~B() { cout << "~B "; }
};
int main() {
B b;
}Show Answer & Explanation
Correct Answer: C - ~B ~A
Destructors are called in reverse order of construction. The derived class destructor ~B is called first, then the base class destructor ~A. Output: '~B ~A'.
Show Answer & Explanation
Correct Answer: C - A class with at least one pure virtual function
An abstract class contains at least one pure virtual function (declared with = 0). It cannot be instantiated directly and must be inherited by a derived class that implements the pure virtual functions.
Show Answer & Explanation
Correct Answer: D - Yes, structs support multiple inheritance
In C++, structs support all types of inheritance including multiple inheritance. The only difference between struct and class is the default access specifier (public vs private).
What will be the output?
class A {
public:
int x;
A() : x(1) {}
};
class B : virtual public A {
public:
B() { x = 2; }
};
class C : virtual public A {
public:
C() { x = 3; }
};
class D : public B, public C {
public:
D() {}
};
int main() {
D d;
cout << d.x;
}Show Answer & Explanation
Correct Answer: A - 3
With virtual inheritance, there is only one copy of A. Constructors are called in order: A(), B(), C(). C's constructor sets x = 3 last, so d.x = 3.
Show Answer & Explanation
Correct Answer: D - Inheritance
Inheritance represents an 'is-a' relationship (e.g., a Dog is an Animal). Composition represents a 'has-a' relationship (e.g., a Car has an Engine).
What will be the output?
class Base {
public:
Base() { cout << "B"; }
virtual ~Base() { cout << "~B"; }
};
class Mid : virtual public Base {
public:
Mid() { cout << "M"; }
~Mid() { cout << "~M"; }
};
class Derived : public Mid {
public:
Derived() { cout << "D"; }
~Derived() { cout << "~D"; }
};
int main() {
Derived d;
}Show Answer & Explanation
Correct Answer: B - BMD~D~M~B
Constructors are called base to derived: Base ('B'), Mid ('M'), Derived ('D'). Destructors are called in reverse: ~Derived ('~D'), ~Mid ('~M'), ~Base ('~B'). Output: 'BMD~D~M~B'.