250+ TOP MCQs on Abstract Classes – 1 and Answers

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

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

1. Which class is used to design the base class?
a) abstract class
b) derived class
c) base class
d) derived & base class
Answer: a
Clarification: Abstract class is used to design base class because functions of abstract class can be overridden in derived class hence derived class from same base class can have common method with different implementation, hence forcing encapsulation.

2. Which is used to create a pure virtual function?
a) $
b) =0
c) &
d) !
Answer: b
Clarification: For making a method as pure virtual function, We have to append ‘=0’ to the class or method.

3. Which is also called as abstract class?
a) virtual function
b) pure virtual function
c) derived class
d) base class
Answer: b
Clarification: Classes that contain at least one pure virtual function are called as abstract base classes.

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

  1.     #include 
  2.     using namespace std;
  3.     class p 
  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.         virtual int area (void) = 0;
  13.     };
  14.     class r: public p
  15.     {
  16.         public:
  17.         int area (void)
  18.         { 
  19.             return (width * height);
  20.         }
  21.     };
  22.     class t: public p 
  23.     {
  24.         public:
  25.         int area (void)
  26.         {
  27.             return (width * height / 2); 
  28.         }
  29.     };
  30.     int main () 
  31.     {
  32.         r rect;
  33.         t trgl;
  34.         p * ppoly1 = ▭
  35.         p * ppoly2 = &trgl;
  36.         ppoly1->set_values (4, 5);
  37.         ppoly2->set_values (4, 5);
  38.         cout << ppoly1 -> area() ;
  39.         cout << ppoly2 -> area();
  40.         return 0;
  41.     }

a) 1020
b) 20
c) 10
d) 2010
Answer: d
Clarification: In this program, We are calculating the area of rectangle and
triangle by using abstract class.
Output:

$ g++ abs.cpp
$ a.out
2010

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

  1.     #include 
  2.     using namespace std;
  3.     class MyInterface 
  4.     {
  5.         public:
  6.         virtual void Display() = 0;
  7.     };
  8.     class Class1 : public MyInterface 
  9.     {
  10.         public:
  11.         void Display() 
  12.         {
  13.             int  a = 5;
  14.             cout << a;
  15.         }
  16.     };
  17.     class Class2 : public MyInterface 
  18.     {
  19.         public:
  20.         void Display()
  21.         {
  22.             cout <<" 5" << endl;
  23.         }
  24.     };
  25.     int main()
  26.     {
  27.         Class1 obj1;
  28.         obj1.Display();
  29.         Class2 obj2;
  30.         obj2.Display();
  31.         return 0;
  32.     }

a) 5
b) 10
c) 5 5
d) 15
Answer: c
Clarification: In this program, We are displaying the data from the two classes
by using abstract class.
Output:

$ g++ abs1.cpp
$ a.out
5 5

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

  1.     #include 
  2.     using namespace std;
  3.     class sample
  4.     {
  5.         public:
  6.         virtual void example() = 0; 
  7.     };
  8.     class Ex1:public sample
  9.     {
  10.         public:
  11.         void example()
  12.         {
  13.             cout << "ubuntu";
  14.         }
  15.     };
  16.     class Ex2:public sample
  17.     {
  18.         public:
  19.         void example()
  20.         {
  21.             cout << " is awesome";
  22.         }
  23.     };
  24.     int main()
  25.     {
  26.         sample* arra[2];
  27.         Ex1 e1;
  28.         Ex2 e2;
  29.         arra[0]=&e1;
  30.         arra[1]=&e2;
  31.         arra[0]->example();
  32.         arra[1]->example();
  33.     }

a) ubuntu
b) is awesome
c) ubuntu is awesome
d) ubunt esome
Answer: c
Clarification: In this program, We are combining the two statements from two classes and printing it by using abstract class.
Output:

$ g++ abs3.cpp
$ a.out
ubuntu is awesome