250+ TOP MCQs on Class Hierarchies and Abstract Classes and Answers

This section on C++ programming questions and answers on “Class Hierarchies and Abstract Classes”. 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++ programming questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

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

1. How many kinds of classes are there in c++?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two kinds of classes in c++. They are absolute class and the concrete class.

2. What is meant by polymorphism?
a) class having many forms
b) class having only single form
c) class having two forms
d) class having four forms
Answer: a
Clarification: Polymorphism is literally meant class having many forms.

3. How many types of inheritance are there in c++?
a) 2
b) 3
c) 4
d) 5
Answer: d
Clarification: There are five types of inheritance in c++. They are single, Multiple, Hierarchical, Multilevel, Hybrid.

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

  1.     #include 
  2.     using namespace std;
  3.     class stu
  4.     {
  5.         protected:
  6.         int rno;
  7.         public:
  8.         void get_no(int a)
  9.         {
  10.             rno = a;
  11.         }
  12.         void put_no(void)
  13.         {
  14.         }
  15.     };
  16.     class test:public stu
  17.     {
  18.         protected:
  19.         float part1,part2;
  20.         public:
  21.         void get_mark(float x, float y)
  22.         {
  23.             part1 = x;
  24.             part2 = y;
  25.         }
  26.         void put_marks()
  27.         {
  28.         }
  29.     };
  30.     class sports
  31.     {
  32.         protected:
  33.         float score;
  34.         public:
  35.         void getscore(float s)
  36.         {
  37.             score = s;
  38.         }
  39.         void putscore(void)
  40.         {
  41.         }
  42.     };
  43.     class result: public test, public sports
  44.     {
  45.         float total;
  46.         public:
  47.         void display(void);
  48.     };
  49.     void result::display(void)
  50.     {
  51.         total = part1 + part2 + score;
  52.         put_no();
  53.         put_marks();
  54.         putscore();
  55.         cout << "Total Score=" << total << "n";
  56.     }
  57.     int main()
  58.     {
  59.         result stu;
  60.         stu.get_no(123);
  61.         stu.get_mark(27.5, 33.0);
  62.         stu.getscore(6.0);
  63.         stu.display();
  64.         return 0;
  65.     }

a) 66.5
b) 64.5
c) 62.5
d) 60.5
Answer: a
Clarification: In this program, We are passing the values by using different methods and totaling the marks to get the result.
Output:

$ g++ class.cpp
$ a.out
Total Score=66.5

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

  1.     #include 
  2.     using namespace std;
  3.     class poly
  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.     };
  13.     class Coutput
  14.     {
  15.         public:
  16.         void output(int i);
  17.     };
  18.     void Coutput::output(int i)
  19.     {
  20.         cout << i;
  21.     }
  22.     class rect:public poly, public Coutput
  23.     {
  24.         public:
  25.         int area()
  26.         {
  27.             return(width * height);
  28.         }
  29.     };
  30.     class tri:public poly, public Coutput
  31.     {
  32.         public:
  33.         int area()
  34.         {
  35.             return(width * height / 2);
  36.         }
  37.     };
  38.     int main()
  39.     {
  40.         rect rect;
  41.         tri trgl;
  42.         rect.set_values(3, 4);
  43.         trgl.set_values(4, 5);
  44.         rect.output(rect.area());
  45.         trgl.output(trgl.area());
  46.         return 0;
  47.     }

a) 1212
b) 1210
c) 1010
d) 1250
Answer: b
Clarification: In this program, We are calculating the area of rectangle and triangle by using multilevel inheritance.

$ g++ class1.cpp
$ a.out
1210

6. What is meant by container ship?
a) class contains objects of other class types as its members
b) class contains objects of other class types as its objects
c) class contains objects of other class types as its members 7 also objects
d) class contains objects of other class types as its members 9 also objects
Answer: a
Clarification: Container ship is a class contains objects of other class types as its members.

7. How many types of the constructor are there in C++?
a) 1
b) 2
c) 3
d) 4
Answer: c
Clarification: There are three types of constructor in C++. They are the Default constructor, Parameterized constructor, Copy constructor.

8. How many constructors can present in a class?
a) 1
b) 2
c) 3
d) multiple
Answer: d
Clarification: There can be multiple constructors of the same class, provided they have different signatures.

9. What should be the name of the constructor?
a) same as the object
b) same as the member
c) same as the class
d) same as the function
Answer: c
Clarification: Constructor name should be same as the class name.

10. What does derived class does not inherit from the base class?
a) constructor and destructor
b) friends
c) operator = () members
d) all of the mentioned
Answer: d
Clarification: The derived class inherits everything from the base class except the given things.