Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Types of Inheritance”.
1. How many types of inheritance are possible in C++?
a) 2
b) 3
c) 4
d) 5
Answer: d
Clarification: There are five types of inheritance that are possible in C++. Single level, Multilevel, multiple, hierarchical and hybrid. Here we count hybrid also because it sometimes can bring up a new form of inheritance, Like inheritance using multiple and hierarchical, which sometimes results in diamond problem.
2. Which among the following is true?
a) Java supports all types of inheritance
b) Java supports multiple inheritance
c) Java doesn’t support multiple inheritance
d) Java doesn’t support inheritance
Answer: c
Clarification: Java doesn’t support multiple inheritance. This is done to avoid the diamond problem that sometimes arises with inherited functions. Though, multiple inheritance can be implemented in java using interfaces.
3. Which type of inheritance is illustrated by the following code?
class student{ public: int marks; }; class topper: public student { public: char grade; }; class average{ public: int makrs_needed; }; class section: public average{ public: char name[10]; }; class overall: public average{ public: int students; };
a) Single level
b) Multilevel and single level
c) Hierarchical
d) Hierarchical and single level
Answer: c
Clarification: It is hierarchical inheritance and single level inheritance. Since class topper is inheriting class student, it is single level inheritance. And then average is inherited by section and overall, so it is hierarchical inheritance. But both of them are separate. Hence it is not hybrid inheritance.
4. Which among the following best describes multiple inheritance?
a) Two classes being parent of any other classes
b) Three classes being parent of other classes
c) More than one class being parent of other child classes
d) More than one class being parent of single child
Answer: d
Clarification: If a class inherits more than one class, it is known as multiple inheritance. This should not be referred with only two or three classes being inherited. But there must be one class which inherits more than one class to be called as multiple inheritance.
5. How many types of inheritance can be used at a time in a single program?
a) Any two types
b) Any three types
c) Any 4 types
d) Any type, any number of times
Answer: d
Clarification: Any type of inheritance can be used in any program. There is no rule to use only few types of inheritance. Only thing that matters is how the classes are inherited and used.
6. Which type of inheritance results in the diamond problem?
a) Single level
b) Hybrid
c) Hierarchical
d) Multilevel
Answer: b
Clarification: In diamond problem, hierarchical inheritance is used first, where two different classes inherit the same class and then in turn a 4th class inherits the two classes which had inherited the first class. Using more than one type of inheritance here, it is known as hybrid inheritance.
7. If 6 classes uses single level inheritance with pair classes (3 pairs), which inheritance will this be called?
a) Single
b) Multiple
c) Hierarchical
d) Multilevel
Answer: a
Clarification: Here all the pairs are using single inheritance. And no different pairs are inheriting same classes. Hence it can’t be called hybrid or multilevel inheritance. You can say the single inheritance is used 3 times in that program.
8. Which among the following is correct for the following code?
class A { public : class B { public : B(int i): data(i) { } int data; } }; class C: public A { class D:public A::B{ }; };
a) Multi-level inheritance is used, with nested classes
b) Multiple inheritance is used, with nested classes
c) Single level inheritance is used, with enclosing classes
d) Single level inheritance is used, with both enclosing and nested classes
