C++ Programming MCQ – Multiple Inheritance

This set of C++ programming Multiple Choice Questions & Answers (MCQs) focuses on “Multiple Inheritance”. 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 “Multiple Inheritance” along with answers, explanations and/or solutions:

1. What is meant by multiple inheritance?
a) Deriving a base class from derived class
b) Deriving a derived class from base class
c) Deriving a derived class from more than one base class
d) Deriving a derived base class
View Answer

Answer: c
Explanation: Multiple inheritance enables a derived class to inherit members from more than one parent.

2. Which symbol is used to create multiple inheritances?
a) Dot
b) Comma
c) Dollar
d) star
View Answer

Answer: b
Explanation: For using multiple inheritance, simply specify each base class (just like in single inheritance), separated by a comma.

3. Which of the following advantages we lose by using multiple inheritances?
a) Dynamic binding
b) Polymorphism
c) Both Dynamic binding & Polymorphism
d) Constructor
View Answer

Answer: c
Explanation: The benefit of dynamic binding and polymorphism is that they help making the code easier to extend but by multiple inheritance it makes harder to track.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     class polygon 
  4.     {
  5.         protected:
  6.         int width, height;
  7.         public:
  8.         void set_values (int a, int b)
  9.         { 
  10.             width = a; height = b;}
  11.         };
  12.         class output1 
  13.         {
  14.             public:
  15.                 void output (int i);
  16.         };
  17.     void output1::output (int i) 
  18.     {
  19.         cout << i << endl;
  20.     }
  21.     class rectangle: public polygon, public output1 
  22.     {
  23.         public:
  24.         int area ()
  25.         { 
  26.             return (width * height); 
  27.         }
  28.     };
  29.     class triangle: public polygon, public output1 
  30.     {
  31.         public:
  32.         int area ()
  33.         {
  34.             return (width * height / 2); 
  35.         }
  36.     };
  37.     int main () 
  38.     {
  39.         rectangle rect;
  40.         triangle trgl;
  41.         rect.set_values (4, 5);
  42.         trgl.set_values (4, 5);
  43.         rect.output (rect.area());
  44.         trgl.output (trgl.area());
  45.         return 0;
  46.     }

a) 20
b) 10
c)

   20
   10
Note: Join free Sanfoundry classes at Telegram or Youtube

d) 30
View Answer

Answer: c
Explanation: We are using the multiple inheritance to find the area of rectangle and triangle.
Output:

advertisement
$ g++ mul.cpp
$ a.out
20
10

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

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     class Base
  4.     {
  5.         public:
  6.         virtual void print() const = 0;
  7.     };
  8.     class DerivedOne : public Base
  9.     {
  10.         public:     
  11.         void print() const
  12.         {
  13.             cout << "DerivedOne\n";
  14.         }
  15.     };
  16.     class DerivedTwo : public Base
  17.     {
  18.         public:
  19.         void print() const
  20.         {
  21.             cout << "DerivedTwo\n";
  22.         }     
  23.     }; 
  24.     class Multiple : public DerivedOne, public DerivedTwo
  25.     {
  26.         public:
  27.         void print() const
  28.         {
  29.             DerivedTwo :: print();
  30.         } 
  31.     }; 
  32.     int main()
  33.     {
  34.         int i;
  35.         Multiple both; 
  36.         DerivedOne one; 
  37.         DerivedTwo two; 
  38.         Base *array[ 3 ]; 
  39.         array[ 0 ] = &both; 
  40.         array[ 1 ] = &one;
  41.         array[ 2 ] = &two;
  42.         array[ i ] -> print();
  43.         return 0;
  44.     }

a) DerivedOne
b) DerivedTwo
c) Error
d) DerivedThree
View Answer

Answer: c
Explanation: In this program, ‘Base’ is an ambiguous base of ‘Multiple’. So it is producing an error. And this program is a virtual base class.

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     class student
  4.     {
  5.         public:
  6.         int rno , m1 , m2 ;
  7.         void get()
  8.         {
  9.             rno = 15, m1 = 10, m2 = 10;
  10.         }
  11.     };
  12.     class sports
  13.     {
  14.         public:
  15.         int sm;
  16.         void getsm()
  17.         {
  18.             sm = 10;
  19.         }
  20.     };
  21.     class statement:public student,public sports
  22.     {
  23.         int tot,avg;
  24.         public:
  25.         void display()
  26.         {
  27.             tot = (m1 + m2 + sm);
  28.             avg = tot / 3;
  29.             cout << tot;
  30.             cout << avg;
  31.         }
  32.     };
  33.     int main()
  34.     {
  35.         statement obj;
  36.         obj.get();
  37.         obj.getsm();
  38.         obj.display();
  39.     }

a) 3100
b) 3010
c) 2010
d) 1010
View Answer

Answer: b
Explanation: In this program, We are calculating the total and average marks of a student by using multiple inheritance.
Output:

$ g++ mul1.cpp
$ a.out
3010

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     struct a
  4.     {
  5.         int count;
  6.     };
  7.     struct b
  8.     {
  9.         int* value;
  10.     };
  11.     struct c : public a, public b
  12.     {
  13.     };
  14.     int main()
  15.     {
  16.         c* p = new c;
  17.         p->value = 0;
  18.         cout << "Inherited";
  19.         return 0;
  20.     }

a) Inherited
b) Error
c) Runtime error
d) inherited
View Answer

Answer: a
Explanation: In this program, We apply the multiple inheritance to structure.
Output:

$ g++ mul2.cpp
$ a.out
Inherited

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Base1
  4.     {
  5.         protected:
  6.         int SampleDataOne;
  7.         public:
  8.         Base1()
  9.         {
  10.             SampleDataOne = 100;
  11.         }
  12.         ~Base1()
  13.         {
  14.         }
  15.         int SampleFunctOne()
  16.         {
  17.             return SampleDataOne;
  18.         }
  19.     };
  20.     class Base2
  21.     {
  22.         protected:
  23.         int SampleDataTwo;
  24.         public:
  25.         Base2()
  26.         {
  27.             SampleDataTwo = 200;
  28.         }
  29.         ~Base2()
  30.         {
  31.         }
  32.         int SampleFunctTwo()
  33.         {
  34.             return SampleDataTwo;
  35.         }
  36.     };
  37.     class Derived1 : public Base1, public Base2
  38.     {
  39.         int MyData;
  40.         public:
  41.         Derived1() 
  42.         {
  43.             MyData = 300;
  44.         }
  45.         ~Derived1()
  46.         {
  47.         }    
  48.         int MyFunct()
  49.         {
  50.             return (MyData + SampleDataOne + SampleDataTwo);
  51.         }
  52.     };
  53.     int main()
  54.     {
  55.         Base1 SampleObjOne;
  56.         Base2 SampleObjTwo;
  57.         Derived1 SampleObjThree;
  58.         cout << SampleObjThree.Base1 :: SampleFunctOne() << endl;
  59.         cout << SampleObjThree.Base2 :: SampleFunctTwo() << endl;
  60.         return 0;
  61.     }

a) 100
b) 200
c) Both 100 & 200
d) 150
View Answer

Answer: c
Explanation: In this program, We are passing the values by using multiple inheritance and printing the derived values.
Output:

$ g++ mul4.cpp
$ a.out
100
200

9. Which design patterns benefit from the multiple inheritances?
a) Adapter and observer pattern
b) Code pattern
c) Glue pattern
d) Star pattern
View Answer

Answer: a
Explanation: Adapter and observer patterns benefit from the multiple inheritances.

10. What are the things are inherited from the base class?
a) Constructor and its destructor
b) Operator=() members
c) Friends
d) All of the mentioned
View Answer

Answer: d
Explanation: These things can provide necessary information for the base class to make a logical decision.

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.