250+ TOP MCQs on Derived Classes and Answers

This section on C++ programming questions and answers on “Derived Classes”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ programming questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ programming questions on “Derived Classes” along with answers, explanations and/or solutions:

1. Where is the derived class is derived from?
a) derived
b) base
c) both derived & base
d) class
Answer: b
Clarification: Because derived inherits functions and variables from base.

2. Pick out the correct statement.
a) A derived class’s constructor cannot explicitly invokes its base class’s constructor
b) A derived class’s destructor cannot invoke its base class’s destructor
c) A derived class’s destructor can invoke its base class’s destructor
d) A derived class’s destructor can invoke its base & derived class’s destructor
Answer: b
Clarification: Destructors are automatically invoked when an object goes out of scope or when a dynamically allocated object is deleted. Inheritance does not change this behavior. This is the reason a derived destructor cannot invoke its base class destructor.

3. Which of the following can derived class inherit?
a) members
b) functions
c) both members & functions
d) classes
Answer: c
Clarification: Both data members and member functions are inherited by derived class in C++.

4. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     class A
  4.     {
  5.         public:
  6.         A(int n )
  7.         {
  8.             cout << n;
  9.         }
  10.     };
  11.     class B: public A
  12.     {
  13.         public:
  14.         B(int n, double d)
  15.         : A(n)
  16.         {
  17.             cout << d;
  18.         }    
  19.     };
  20.     class C: public B
  21.     {
  22.         public:
  23.         C(int n, double d, char ch)
  24.         : B(n, d)
  25.         {
  26.             cout <<ch;
  27.         }
  28.     };
  29.     int main()
  30.     {
  31.         C c(5, 4.3, 'R');
  32.         return 0;
  33.     }

a) 54.3R
b) R4.35
c) 4.3R5
d) R2.6
Answer: a
Clarification: In this program, We are passing the value and manipulating by using the derived class.
Output:

$ g++ der.cpp
$ a.out
54.3R

5. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     class BaseClass 
  4.     {
  5.         protected:
  6.         int i;
  7.         public:
  8.         BaseClass(int x) 
  9.         {
  10.             i = x;
  11.         }
  12.         ~BaseClass() 
  13.         {
  14.         }
  15.     };
  16.     class DerivedClass: public BaseClass 
  17.     {
  18.         int j;
  19.         public:
  20.         DerivedClass(int x, int y): BaseClass(y)
  21.         {
  22.             j = x;
  23.         }
  24.         ~DerivedClass() 
  25.         {
  26.         }
  27.         void show() 
  28.         {
  29.             cout << i << " " << j << endl;
  30.         }
  31.     };
  32.     int main()
  33.     {
  34.         DerivedClass ob(3, 4);
  35.         ob.show();
  36.         return 0;
  37.     }

a) 3 4
b) 4 3
c) 4
d) 3
Answer: b
Clarification: In this program, We are passing the values and assigning it to i and j and we are printing it.
Output:

$ g++ der1.cpp
$ a.out
4 3

6. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     class Base
  4.     {
  5.         public:
  6.         int m;
  7.         Base(int n=0)
  8.         : m(n)
  9.         {
  10.             cout << "Base" << endl;
  11.         }
  12.     };
  13.     class Derived: public Base
  14.     {
  15.         public:
  16.         double d;
  17.         Derived(double de = 0.0)
  18.         : d(de)
  19.         {
  20.             cout << "Derived" << endl;
  21.         }
  22.     };
  23.     int main()
  24.     {
  25.         cout << "Instantiating Base" << endl;
  26.         Base cBase;
  27.         cout << "Instantiating Derived" << endl;
  28.         Derived cDerived;
  29.         return 0;
  30.     }

a)

   Instantiating Base
   Base
   Instantiating Derived
   Base
   Derived

b)

   Instantiating Base
   Instantiating Derived
   Base 
   Derived

c)

   Instantiating Base
   Base
   Instantiating Derived
   Base

d) Instantiating Base
Answer: a
Clarification: In this program, We are printing the execution order of the program.
Output:

$ g++ der2.cpp
$ a.out
Instantiating Base
Base
Instantiating Derived
Base
Derived