250+ TOP MCQs on Design of Class Hierarchies and Answers

This section on online C++ quiz on “Design of Class Hierarchies”. One shall practice these online quizzes 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++ quiz comes with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of online C++ quiz 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: Class hierarchies promote reuse in two ways. They are code sharing and interface sharing.

  250+ TOP MCQs on Iterators and Answers

4. How many types of class are there in c++?
a) 1
b) 2
c) 3
d) 4
Answer: c
Clarification: 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?

  1.     #include 
  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
Answer: c
Clarification: In this program, We are multiplying the value 10 and 4 by using inheritance.
Output:

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
Answer: b
Clarification: In multiple inheritances, We are able to derive a class from more than one base class.

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

  1.     #include 
  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
Answer: c
Clarification: 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 
  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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: Syntax is:
class Class_Name: Access_Specifier Base_Class_Name

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top