C++ Programming Questions and Answers – Design of Class Hierarchies

This section on online C++ Multiple Choice Questions focuses on “Design of Class Hierarchies”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in 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++ questions comes with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ Multiple Choice Questions & Answers focuses on “Design of Class Hierarchies” along with answers, explanations and/or solutions:

1. Which interface determines how your class will be used by another program?
a) public
b) private
c) protected
d) void
View Answer

Answer: a
Explanation: If we invoked the interface as public means, We can access the program from other programs also.

2. Pick out the correct statement about the override.
a) Overriding refers to a derived class function that has the same name and signature as a base class virtual function
b) Overriding has different names
c) Overriding refers to a derived class
d) Overriding has different names & it refers to a derived class
View Answer

Answer: a
Explanation: Overriding refers to a derived class function that has the same name and signature as a base class virtual function.

3. How many ways of reusing are there in the class hierarchy?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: Class hierarchies promote reuse in two ways. They are code sharing and interface sharing.
advertisement
advertisement

4. How many types of class are there in c++?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: There are three types of classes. They are abstract base classes, concrete derived classes, standalone classes.

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

Note: Join free Sanfoundry classes at Telegram or Youtube
  1.     #include <iostream>
  2.     using namespace std;
  3.     class BaseClass 
  4.     {
  5.         int i;
  6.         public:
  7.         void setInt(int n);
  8.         int getInt();
  9.     };
  10.     class DerivedClass : public BaseClass
  11.     {
  12.         int j;
  13.         public:
  14.         void setJ(int n);
  15.         int mul();
  16.     };
  17.     void BaseClass::setInt(int n)
  18.     {
  19.         i = n;
  20.     }
  21.     int BaseClass::getInt()
  22.     {
  23.         return i;
  24.     }
  25.     void DerivedClass::setJ(int n)
  26.     {
  27.         j = n;
  28.     }
  29.     int DerivedClass::mul()
  30.     {
  31.         return j * getInt();
  32.     }
  33.     int main()
  34.     {
  35.         DerivedClass ob;
  36.         ob.setInt(10);       
  37.         ob.setJ(4);          
  38.         cout << ob.mul();    
  39.         return 0;
  40.     }

a) 10
b) 4
c) 40
d) 30
View Answer

Answer: c
Explanation: In this program, We are multiplying the value 10 and 4 by using inheritance.
Output:

advertisement
$ g++ des.cpp
$ a.out
40

6. Pick out the correct statement about multiple inheritances.
a) Deriving a class from one direct base class
b) Deriving a class from more than one direct base class
c) Deriving a class from more than one direct derived class
d) Deriving a class from more than one direct derivedbase class
View Answer

Answer: b
Explanation: In multiple inheritances, We are able to derive a class from more than one base class.
advertisement

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     class BaseClass 
  4.     {
  5.         int x;
  6.         public:
  7.         void setx(int n) 
  8.         {
  9.             x = n;
  10.         }
  11.         void showx() 
  12.         {
  13.             cout << x ;
  14.         }
  15.     };
  16.     class DerivedClass : private BaseClass
  17.     {
  18.         int y;
  19.         public:
  20.         void setxy(int n, int m)
  21.         {
  22.             setx(n);      
  23.             y = m;
  24.         }
  25.         void showxy() 
  26.         {
  27.             showx();       
  28.             cout << y << '\n';
  29.         }
  30.     };
  31.     int main()
  32.     {
  33.         DerivedClass ob;
  34.         ob.setxy(10, 20);
  35.         ob.showxy();
  36.         return 0;
  37.     }

a) 10
b) 20
c) 1020
d) 1120
View Answer

Answer: c
Explanation: In this program, We are passing the values from the main class and printing it on the inherited classes.
Output:

$ g++ des2.cpp
$ a.out
1020

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     class BaseClass 
  4.     {
  5.         public:
  6.         virtual void myFunction()
  7.         {
  8.             cout << "1";
  9.         }
  10.     };
  11.     class DerivedClass1 : public BaseClass 
  12.     {
  13.         public:
  14.         void myFunction()
  15.         {
  16.             cout << "2";
  17.         }
  18.     };
  19.    class DerivedClass2 : public DerivedClass1 
  20.     {
  21.         public:
  22.         void myFunction()
  23.         {
  24.             cout << "3";
  25.         }
  26.     };
  27.     int main()
  28.     {
  29.         BaseClass *p;
  30.         BaseClass ob;
  31.         DerivedClass1 derivedObject1;
  32.         DerivedClass2 derivedObject2;
  33.         p = &ob;
  34.         p -> myFunction();
  35.         p = &derivedObject1;
  36.         p -> myFunction();
  37.         p = &derivedObject2;
  38.         p -> myFunction();
  39.         return 0;
  40.     }

a) 123
b) 12
c) 213
d) 321
View Answer

Answer: a
Explanation: We are passing the objects and executing them in a certain order and we are printing the program flow.
Output:

$ g++ des3.cpp
$ a.out
123

9. What does inheritance allow you to do?
a) create a class
b) create a hierarchy of classes
c) access methods
d) create a method
View Answer

Answer: b
Explanation: Inheritance helps in creating hierarchy of classes by making connections between different classes in which one is called base class and other is class derived class.

10. What is the syntax of inheritance of class?
a) class name
b) class name: access specifier
c) class name: access specifier class name
d) access specifier class name
View Answer

Answer: c
Explanation: Syntax is:
class Class_Name: Access_Specifier Base_Class_Name

example:
class A{};
class B: public A{};

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.

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.