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
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
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
Explanation: 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?
#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) 66.5
b) 64.5
c) 62.5
d) 60.5
View Answer
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?
#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
View Answer
Explanation: 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
View Answer
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
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
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
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
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]
- Practice Programming MCQs
- Check C++ Books
- Check Programming Books
- Check Computer Science Books
- Practice Computer Science MCQs