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

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.
C++ programming questions on “Class Hierarchies and Abstract Classes” along with answers, explanations and/or solutions:

Q 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

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


Q 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

Show Answer Answer:-A. class having many forms
Explanation Polymorphism is literally meant class having many forms.


Q 3. How many types of inheritance are there in c++?
A. 2
B. 3
C. 4
D. 5

Show Answer Answer:-D. 5
ExplanationThere are five types of inheritance in c++. They are single, Multiple, Hierarchical, Multilevel, Hybrid.

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

#include <iostream>
using namespace std;
class stu
{
protected:
int rno;
public:
void get_no(int a)
{
rno = a;
}
void put_no(void)
{
}
};
class test:public stu
{
protected:
float part1,part2;
public:
void get_mark(float x, float y)
{
part1 = x;
part2 = y;
}
void put_marks()
{
}
};
class sports
{
protected:
float score;
public:
void getscore(float s)
{
score = s;
}
void putscore(void)
{
}
};
class result: public test, public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total = part1 + part2 + score;
put_no();
put_marks();
putscore();
cout << “Total Score=” << total << “\n”;
}
int main()
{
result stu;
stu.get_no(123);
stu.get_mark(27.5, 33.0);
stu.getscore(6.0);
stu.display();
return 0;
}
A. 60.5
B. 62.5
C. 64.5
D. 66.5

Show Answer Answer:-D. 66.5
ExplanationIn this program, We are passing the values by using different methods and totaling the marks to get the result. Output: Subscribe Now: C++ Newsletter | Important Subjects Newsletters $ g++ class.cpp $ a.out Total Score=66.5

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

#include <iostream>
using namespace std;
class poly
{
protected:
int width, height;
public:
void set_values(int a, int b)
{
width = a; height = b;
}
};
class Coutput
{
public:
void output(int i);
};
void Coutput::output(int i)
{
cout << i;
}
class rect:public poly, public Coutput
{
public:
int area()
{
return(width * height);
}
};
class tri:public poly, public Coutput
{
public:
int area()
{
return(width * height / 2);
}
};
int main()
{
rect rect;
tri trgl;
rect.set_values(3, 4);
trgl.set_values(4, 5);
rect.output(rect.area());
trgl.output(trgl.area());
return 0;
}
A. 1212
B. 1210
C. 1010
D. 1250

Show Answer Answer:-B. 1210
Explanation In this program, We are calculating the area of rectangle and triangle by using multilevel inheritance. $ g++ class1.cpp $ a.out 1210


Q 6. What is meant by container ship?
A. class contains objects of other class types as its objects
B. class contains objects of other class types as its members
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

Show Answer Answer:-B. class contains objects of other class types as its members
ExplanationContainer ship is a class contains objects of other class types as its members.

Q 7. How many types of the constructor are there in C++?
A. 1
B. 2
C. 3
D. 4

Show Answer Answer:-C. 3
ExplanationThere are three types of constructor in C++. They are the Default constructor, Parameterized constructor, Copy constructor.


Q 8. How many constructors can present in a class?
A. 1
B. 2
C. 3
D. multiple

Show Answer Answer:-D. multiple
ExplanationThere can be multiple constructors of the same class, provided they have different signatures.


Q 9. What should be the name of the constructor?
A. same as the object
B. same as the class
C. same as the member
D. same as the function

Show Answer Answer:-B. same as the class
Explanation Constructor name should be same as the class name.


Q 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

Show Answer Answer:-D. all of the mentioned
Explanation The derived class inherits everything from the base class except the given things.

Posted

in

by

Comments

Leave a Reply

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

You cannot copy content of this page