Object Oriented Programming using C++ Questions and Answers – Hierarchical Inheritance

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Hierarchical Inheritance”.

1. Which among the following is best to define hierarchical inheritance?
a) More than one classes being derived from one class
b) More than 2 classes being derived from single base class
c) At most 2 classes being derived from single base class
d) At most 1 class derived from another class
View Answer

Answer: a
Explanation: When two or more classes get derived from a single base class, it is known as hierarchical inheritance. This gives us freedom to use same code with different scopes and flexibility into different classes.

2. Do members of base class gets divided among all of its child classes?
a) Yes, equally
b) Yes, depending on type of inheritance
c) No, it’s doesn’t get divided
d) No, it may or may not get divided
View Answer

Answer: c
Explanation: The class members doesn’t get divided among the child classes. All the members get derived to each of the subclasses as whole. The only restriction is from the access specifiers used.

3. Each class can inherit the base class ________________
a) Independently using any inheritance
b) Independently with private inheritance only
c) With same type of inheritance
d) With each class using different inheritance only
View Answer

Answer: a
Explanation: The classes can inherit the base class using any type of inheritance. There is no mandatory condition to use same private,public or protected inheritance only.
advertisement
advertisement

4. How many classes must be there to implement hierarchical inheritance?
a) Exactly 3
b) At least 3
c) At most 3
d) At least 1
View Answer

Answer: b
Explanation: At least 3 classes must be there. Two derived classes and one base class. This lets us implement two classes that have common characteristics from base class.

5. Base class _______________
a) Can be made abstract
b) Can’t be made abstract
c) Must be abstract
d) If made abstract, compile time error
View Answer

Answer: a
Explanation: The base class may or may not be declared abstract. It depends on the need of program. If it is made abstract, it can contain undefined functions too. In turn, those functions will have to be implemented by each of the derived classes.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. Which access specifiers should be used so that all the derived classes restrict further inheritance of base class members?
a) Private
b) Public
c) Protected
d) Any inheritance type can be used
View Answer

Answer: a
Explanation: All the derived classes must use private inheritance. This will make the members of base class private in derived classes. Hence none of the members of base class will be available for further inheritance.

7. Which class uses hierarchical inheritance in following code?

advertisement
class A
{
	int a;
};
class B:class A
{
	int b;
};
class C:class A,class B
{
	int c;
};
class D:class A
{
	int d;
};

a) Class A, B, C
b) Class B, C, D
c) Class A, C, D
d) Class D, A, B
View Answer

Answer: d
Explanation: Class A is base class and B and D are derived classes. If class C is considered, it shows hybrid inheritance, involving single level and multiple inheritance.
advertisement

8. Which among the following is correct for following code?

abstract class A
{
	public Int a;
	public void disp();
};
class B:public A
{
	public: void dis()
	{ 
		court<<a; 
	}
};
class C:private A
{
	public void incr()
	{ 
		a++; 
	}
}
void main()
{
	B b.disp();
}

a) Compile time error
b) Runtime error
c) Program runs and o/p is 0
d) Program runs and o/p is garbage value
View Answer

Answer: a
Explanation: The derived class D have not implemented the undefined function. Here the main concept involves hierarchical inheritance with abstract base class.

9. How many classes can be derived from the base class using hierarchical inheritance?
a) As many as required
b) Only 7
c) Only 3
d) Up to 127
View Answer

Answer: a
Explanation: The number of classes that can be derived from a base class doesn’t have any restriction and hence will be able to derive as many classes as required. This feature gives more flexibility and code reusability.

10. If one class have derived the base class privately then another class can’t derive the base class publically.
a) True
b) False
View Answer

Answer: b
Explanation: The classes are independent and can access the base class and inherit it in whichever way it is required. The classes can use the base base class members privately or publically maintaining the security of data and methods.

11. Which among the following is true?
a) Hierarchical inheritance is subset of multiple inheritances
b) Hierarchical inheritance is strongest inheritance type
c) Hierarchical inheritance uses only 2 classes for implementation
d) Hierarchical inheritance allows inheritance of common features to more than one class
View Answer

Answer: d
Explanation: Hierarchical inheritance is used to make all the inherited classes have some common features obtained from a single base class. This allows all the classes to maintain a group or to be classified under one class.

12. Hierarchical inheritance can be a subset of _________________
a) Hybrid inheritance
b) Multiple inheritance
c) Single level inheritance
d) Multilevel inheritance
View Answer

Answer: a
Explanation: When we use hybrid inheritance, it can contain any type of inheritance or combination or more than two types. Hence it may contain Hierarchical inheritance too, hence it can be subset of hybrid inheritance.

13. Which type of inheritance is most suitable for inheriting Same syllabus into different colleges with different streams?
a) Multiple
b) Single
c) Hierarchical
d) Multilevel
View Answer

Answer: c
Explanation: When hierarchical inheritance is used, the common syllabus can be adopted into different college classes where the same syllabus is applicable. For changing the syllabus only the details of base class will have to changed.

14. Which class constructor is called first when an object of derived class is created?
a) Base class constructor
b) Derived class constructor
c) Firstly created derived class constructor
d) Last created derived class constructor
View Answer

Answer: a
Explanation: The base class must be initialised first hence the constructor of base class is called first. This makes everything ready for the new object being created.

15. All the derived classes can access only a few members of base class that other derived classes can’t access at same time, in hierarchical inheritance.
a) True
b) False
View Answer

Answer: b
Explanation: The derived classes have full access to all the non private member’s of base class. Every derived class have equal access, none of the class can have special access to specific members of base class in general cases.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using C++, 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.