C++ Programming Questions and Answers – Derived Classes

This section on C++ programming questions and answers focuses 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: Both data members and member functions are inherited by derived class in C++.
advertisement
advertisement

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

  1.     #include <iostream>
  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
View Answer

Answer: a
Explanation: In this program, We are passing the value and manipulating by using the derived class.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ der.cpp
$ a.out
54.3R

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

advertisement
  1.     #include <iostream>
  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
View Answer

Answer: b
Explanation: In this program, We are passing the values and assigning it to i and j and we are printing it.
Output:

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

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

  1.     #include <iostream>
  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
View Answer

Answer: a
Explanation: 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

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Parent
  4.     {
  5.         public:
  6.         Parent (void) 
  7.         {     
  8.             cout << "Parent()\n";
  9.         }
  10.         Parent (int i) 
  11.         { 
  12.             cout << "Parent("<< i << ")\n"; 
  13.         };
  14.         Parent (void) 
  15.         { 
  16.             cout << "~Parent()\n";
  17.         }; 
  18.     };
  19.     class Child1 : public Parent { };
  20.     class Child2 : public Parent
  21.     {
  22.         public:
  23.         Child2 (void) 
  24.         {
  25.             cout << "Child2()\n";
  26.         }
  27.         Child2 (int i) : Parent (i) 
  28.         {
  29.             cout << "Child2(" << i << ")\n"; 
  30.         }
  31.         ~Child2 (void) 
  32.         {
  33.             cout << "~Child2()\n"; 
  34.         }
  35.     };
  36.     int main (void)
  37.     {
  38.         Child1 a;
  39.         Child2 b;
  40.         Child2 c(42);
  41.         return 0;
  42.     }

a)

   Parent()
   Parent()
   Child2()
   Parent(42)
   Child2(42)
   ~Child2()
   ~Parent()
   ~Child2()
   ~Parent()
   ~Parent()

b) error
c) runtime error
d) Parent(42)
View Answer

Answer: b
Explanation: In this program, We got an error in overloading because we didn’t invoke the destructor of parent.

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

  1.     #include<iostream>
  2.     using namespace std;
  3.     class X 
  4.     {
  5.         int m;
  6.         public:
  7.         X() : m(10)
  8.         {                                                       
  9.         }
  10.         X(int mm): m(mm)
  11.         {
  12.         }
  13.         int getm()
  14.         {
  15.             return m;
  16.         }
  17.     };
  18.     class Y : public X 
  19.     {
  20.         int n;
  21.         public:
  22.         Y(int nn) : n(nn) {}                                                
  23.         int getn() { return n; }
  24.     };
  25.     int main()
  26.     {
  27.         Y yobj( 100 );
  28.         cout << yobj.getm() << " " << yobj.getn() << endl;
  29.     }

a) 10 100
b) 100 10
c) 10 10
d) 100 100
View Answer

Answer: a
Explanation: In this program, We are passing the value and getting the result by derived class.
Output:

$ g++ der5.cpp
$ a.out
10 100

9. Which operator is used to declare the destructor?
a) #
b) ~
c) @
d) $
View Answer

Answer: b
Explanation: tilde(~) is used to declare destructor of a class.

10. Which constructor will initialize the base class data member?
a) derived class
b) base class
c) class
d) derived & base class
View Answer

Answer: b
Explanation: Because it is having the proper data set to initialize, Otherwise it will throw an error.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.