C++ Programming Questions and Answers – Class Hierarchies and Abstract Classes

This section on C++ programming questions and answers focuses 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. Which of the following statements regarding absolute and concrete classes in C++ is correct?
a) Absolute classes cannot have concrete subclasses
b) Concrete classes cannot have abstract methods
c) Absolute classes cannot have member variables
d) Concrete classes cannot have constructors
View Answer

Answer: b
Explanation: Concrete classes provide implementations for all their member functions, so they cannot have abstract methods.

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
View Answer

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

Answer: d
Explanation: There are five types of inheritance in c++. They are single, Multiple, Hierarchical, Multilevel, Hybrid.
advertisement
advertisement

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

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

Answer: a
Explanation: 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?

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

Answer: b
Explanation: In this program, We are calculating the area of rectangle and triangle by using multilevel inheritance.

advertisement
$ 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
View Answer

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

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

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

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

Answer: d
Explanation: The derived class inherits everything from the base class except the given things.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.