C++ Programming Questions and Answers – Abstract Classes – 1

This section on C++ interview questions and answers focuses 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
View Answer

Answer: a
Explanation: 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) !
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
Explanation: Classes that contain at least one pure virtual function are called as abstract base classes.
advertisement
advertisement

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

  1.     #include <iostream>
  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 = &rect;
  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
View Answer

Answer: d
Explanation: In this program, We are calculating the area of rectangle and
triangle by using abstract class.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ abs.cpp
$ a.out
2010

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

advertisement
  1.     #include <iostream>
  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
View Answer

Answer: c
Explanation: In this program, We are displaying the data from the two classes
by using abstract class.
Output:

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

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

  1.     #include <iostream>
  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
View Answer

Answer: c
Explanation: 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

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

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

a) 121
b) 212
c) 12
d) 215
View Answer

Answer: b
Explanation: In this program, We are executing these based on the condition given in array. So it is printing as 212.
Output:

$ g++ abs4.cpp
$ a.out
212

8. What is meant by pure virtual function?
a) Function which does not have definition of its own
b) Function which does have definition of its own
c) Function which does not have any return type
d) Function which does not have any return type & own definition
View Answer

Answer: a
Explanation: As the name itself implies, it have to depend on other class only.

9. Pick out the correct option.
a) We cannot make an instance of an abstract base class
b) We can make an instance of an abstract base class
c) We can make an instance of an abstract super class
d) We can make an instance of an abstract derived class
View Answer

Answer: a
Explanation: We cannot make an instance of an abstract base class.

10. Where does the abstract class is used?
a) base class only
b) derived class
c) both derived & base class
d) virtual class
View Answer

Answer: a
Explanation: As base class only as it helps in encapsulation of similar functioning of derived classes.

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.