Back to Practice OOP with C++

Inheritance - Practice MCQs for CCAT

50 Questions Section B: Programming OOP with C++

Inheritance Question Bank for C-CAT

Topic-wise Inheritance MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
Which inheritance leads to diamond problem?
ASingle
BMultilevel
CMultiple
DHierarchical
Show Answer & Explanation

Correct Answer: C - Multiple

Multiple inheritance causes diamond problem when two parent classes inherit from same grandparent.

Q2.
To solve diamond problem, we use:
ASingle inheritance
BPublic inheritance
CVirtual inheritance
DProtected inheritance
Show Answer & Explanation

Correct Answer: C - Virtual inheritance

Virtual inheritance ensures only one copy of base class members.

Q3.
In public inheritance, public members of base become:
APrivate in derived
BProtected in derived
CPublic in derived
DNot inherited
Show Answer & Explanation

Correct Answer: C - Public in derived

In public inheritance, public members remain public in derived class.

Q4.
In private inheritance, public members of base become:
AProtected in derived
BPrivate in derived
CPublic in derived
DNot accessible
Show Answer & Explanation

Correct Answer: B - Private in derived

In private inheritance, public members become private in derived class.

Q5.
Constructor of base class is called:
AAfter derived constructor
BOnly if virtual
CNever
DBefore derived constructor
Show Answer & Explanation

Correct Answer: D - Before derived constructor

Base class constructor is called first, then derived class constructor.

Q6.
Destructor is called in which order?
ABase first
BSimultaneously
CRandom
DDerived first
Show Answer & Explanation

Correct Answer: D - Derived first

Destructors are called in reverse order: derived first, then base.

Q7.
Multilevel inheritance means:
AChain of inheritance (A->B->C)
BMultiple base classes
CMultiple derived classes
DNo inheritance
Show Answer & Explanation

Correct Answer: A - Chain of inheritance (A->B->C)

Multilevel: A derives B, B derives C (chain).

Q8.
Hierarchical inheritance means:
AHybrid
BMultiple base, one derived
CChain
DOne base, multiple derived
Show Answer & Explanation

Correct Answer: D - One base, multiple derived

Hierarchical: multiple classes derive from one base class.

Q9.
Private members of base class are:
AInherited but not accessible
BInherited and accessible
CNot inherited
DAlways public
Show Answer & Explanation

Correct Answer: A - Inherited but not accessible

Private members are inherited but not directly accessible in derived class.

Q10.
Protected members in derived class (public inheritance) are:
APrivate
BPublic
CProtected
DNot inherited
Show Answer & Explanation

Correct Answer: C - Protected

Protected members remain protected in public inheritance.

Q11.
Constructor of base class is called:
AAfter derived constructor
BNever called
CBefore derived constructor
DRandomly
Show Answer & Explanation

Correct Answer: C - Before derived constructor

Base class constructor is always called first before the derived class constructor.

Q12.
Order of destructor call in inheritance:
ABase first, then derived
BOnly base
COnly derived
DDerived first, then base
Show Answer & Explanation

Correct Answer: D - Derived first, then base

Destructors are called in reverse order: derived class destructor first, then base class.

Q13.
Which keyword is used to call base class constructor explicitly?
Abase
BUsing member initializer list
Csuper
Dparent
Show Answer & Explanation

Correct Answer: B - Using member initializer list

In C++, base class constructor is called using member initializer list: Derived() : Base() {}.

Q14.
Hybrid inheritance is:
ACombination of two or more types
BSingle inheritance only
CSame as multiple
DNot allowed in C++
Show Answer & Explanation

Correct Answer: A - Combination of two or more types

Hybrid inheritance combines multiple types of inheritance like hierarchical and multiple.

Q15.
In protected inheritance, public members of base become:
AProtected
BPublic
CPrivate
DNot accessible
Show Answer & Explanation

Correct Answer: A - Protected

In protected inheritance, public members of base class become protected in derived class.

Q16.
final keyword in class declaration:
AMakes class abstract
BCreates singleton
CPrevents inheritance
DMakes all members const
Show Answer & Explanation

Correct Answer: C - Prevents inheritance

A class declared as final cannot be inherited by any other class.

Q17.
Base class pointer can point to:
AOnly base objects
BOnly derived objects
CNeither
DBoth base and derived objects
Show Answer & Explanation

Correct Answer: D - Both base and derived objects

Base class pointer can point to both base and derived class objects (upcasting).

Q18.
Which is NOT inherited by derived class?
APublic members
BConstructors
CProtected members
DStatic members
Show Answer & Explanation

Correct Answer: B - Constructors

Constructors and destructors are not inherited; derived class must define its own.

Q19.
using declaration in inheritance:
ACreates new member
BChanges access of inherited member
CDeletes member
DCreates alias
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.

Q20.
sizeof derived class includes:
ABase members + derived members
BOnly derived members
COnly base members
DNeither
Show Answer & Explanation

Correct Answer: A - Base members + derived members

Size of derived class includes size of base class members plus derived class members.

Q21.

Which type of inheritance is shown below?

class A {};
class B : public A {};
class C : public B {};
ASingle inheritance
BMultilevel inheritance
CMultiple inheritance
DHierarchical inheritance
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.

Q22.
What is the default inheritance access specifier when inheriting a class in C++?
Apublic
Bprotected
Cprivate
Dfriend
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.

Q23.
Which members of a base class are accessible in a derived class with public inheritance?
AOnly public members
BOnly protected members
CAll members including private
DPublic and protected members
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.

Q24.
What is the diamond problem in C++?
AWhen a class has two constructors
BWhen a class inherits from two classes that share a common base class
CWhen two classes have the same name
DWhen a destructor is called twice
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.

Q25.
How is the diamond problem resolved in C++?
AUsing private inheritance
BUsing virtual base classes
CUsing friend functions
DUsing static members
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.

Q26.

What will be the output?

class Base {
public:
  Base() { cout << "Base "; }
};
class Derived : public Base {
public:
  Derived() { cout << "Derived "; }
};
int main() {
  Derived d;
}
ADerived Base
BBase
CBase Derived
DDerived
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'.

Q27.
In which order are destructors called in inheritance?
ABase class first, then derived
BDerived class first, then base
COnly derived class destructor
DRandom order
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.

Q28.
What is the syntax for multiple inheritance in C++?
Aclass C : public A & B {}
Bclass C : public A + B {}
Cclass C extends A, B {}
Dclass C : public A, public B {}
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 {}.

Q29.

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();
}
AA
BB
CAB
DCompilation error
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'.

Q30.
How can you call a base class function that has been overridden in the derived class?
AA::show()
Bbase.show()
Csuper.show()
Dparent.show()
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.

Q31.
What happens to private members of the base class during inheritance?
AThey are inherited but not accessible in derived class
BThey become protected in derived class
CThey become public in derived class
DThey are not inherited at all
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.

Q32.

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;
}
ACBA
BABC
CCAB
DBAC
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'.

Q33.
What is protected inheritance?
AAll members become private
BPublic members of base become protected in derived
COnly protected members are inherited
DProtected members become public
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.

Q34.
Which of the following is NOT a type of inheritance in C++?
ASingle
BMultiple
CHierarchical
DDistributed
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.

Q35.
What is hierarchical inheritance?
AOne class inherits from multiple classes
BClasses inherit in a chain
CMultiple classes inherit from one base class
DOne class inherits from itself
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.

Q36.

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();
}
A10
B0
CCompilation error
DGarbage value
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.

Q37.
What is the correct syntax for virtual base class?
Aclass B : virtual public A {}
Bclass B : public virtual A {}
Cclass B : virtual A {}
DBoth A and B are correct
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.

Q38.

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);
}
A510
B55
C105
DCompilation error
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'.

Q39.
Can a derived class constructor directly initialize private members of the base class?
ANo, it must use the base class constructor
BYes, if the base class allows
CYes, always
DNo, private members cannot be initialized
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.

Q40.
What is hybrid inheritance?
AInheritance using templates
BInheritance using virtual functions only
CInheritance between structs and classes
DCombination of two or more types of inheritance
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.

Q41.

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();
}
ACompilation error (ambiguity)
BA A
CA
DRuntime error
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.

Q42.
How to resolve the ambiguity in the diamond problem without virtual inheritance?
AUsing scope resolution: d.B::display()
BUsing friend functions
CUsing static cast
DIt cannot be resolved without virtual
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().

Q43.
What happens to the access of base class members in private inheritance?
AAll become public
BPublic and protected become private
CAll become protected
DOnly public become private
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.

Q44.
Which keyword is used to prevent a class from being inherited in C++?
Afinal
Bsealed
Cconst
Dstatic
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.

Q45.

What will be the output?

class A {
public:
  ~A() { cout << "~A "; }
};
class B : public A {
public:
  ~B() { cout << "~B "; }
};
int main() {
  B b;
}
A~A ~B
B~B
C~B ~A
D~A
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'.

Q46.
What is an abstract class in terms of inheritance?
AA class that cannot be inherited
BA class with only static members
CA class with at least one pure virtual function
DA class with no data members
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.

Q47.
Is multiple inheritance supported for structs in C++?
ANo, only classes support multiple inheritance
BStructs cannot inherit at all
COnly single inheritance is allowed for structs
DYes, structs support multiple inheritance
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).

Q48.

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;
}
A3
B2
C1
DCompilation error
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.

Q49.
Which of the following best describes the 'is-a' relationship?
AComposition
BAggregation
CFriendship
DInheritance
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).

Q50.

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;
}
ADBM~B~M~D
BBMD~D~M~B
CBDM~M~D~B
DMBD~D~B~M
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'.

Showing 1-10 of 50 questions