This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Single Level Inheritance”.
1. Which among the following defines single level inheritance?
a) One base class derives another class
b) One derived class inherits from one base class
c) One base class inherits from one derived class
d) One derived class derives from another derived class
View Answer
Explanation: If only one base class is used to derive only one subclass, it is known as single level inheritance. The reason of this name is that we inherit the base class to one more level and stop the inheritance any further.
2. If class A and class B are derived from class C and class D, then ________________
a) Those are 2 pairs of single inheritance
b) That is multilevel inheritance
c) Those is enclosing class
d) Those are all independent classes
View Answer
Explanation: Since class A is derived from class C and then class B is derived from class D, there are two pairs of classes which shows single inheritance. Those two pairs are independent of each other though.
3. If single inheritance is used, program will contain ________________
a) At least 2 classes
b) At most 2 classes
c) Exactly 2 classes
d) At most 4 classes
View Answer
Explanation: The program will contain at least 2 classes in the sense of base and derived classes. At least one base class and one derived class must be there. Types of inheritance remains the same though.
4. Single level inheritance supports _____________ inheritance.
a) Runtime
b) Compile time
c) Multiple inheritance
d) Language independency
View Answer
Explanation: The runtime inheritance is done when object of a class is created to call a method. At runtime the function is searched if it is in class of object. If not, it will search in its parent classes and hierarchy for that method.
5. Which method in the code below is single level inherited?
class A { protected int a, b; public: void show() { cout<<a<<b; } }; class B: public A { public: void disp() { cout<<a++<<b++; } }; class C: private A, public B { void avg() { cout<<(a+b)/2; } };
a) Class A
b) Class B
c) Class C
d) None
View Answer
Explanation: Class B is using single level inheritance. Class C is using multiple inheritance. And class A is parent of other two classes.
6. If single level inheritance is used and an abstract class is created with some undefined functions, can its derived class also skip some definitions?
a) Yes, always possible
b) Yes, possible if only one undefined function
c) No, at least 2 undefined functions must be there
d) No, the derived class must implement those methods
View Answer
Explanation: The derived class must implement those methods. This is because the parent class is abstract and hence will have some undefined functions which has to be defined in derived classes. Since we are using single level inheritance, if derived class doesn’t implement those functions then one more class has to be there which will become multi-level inheritance.
7. Which among the following is false for single level inheritance?
a) There can be more than 2 classes in program to implement single inheritance
b) There can be exactly 2 classes to implement single inheritance in a program
c) There can be more than 2 independent classes involved in single inheritance
d) The derived class must implement all the abstract method if single inheritance is used
View Answer
Explanation: If more than 2 independent classes are involved to implement the single level inheritance, it won’t be possible as there must be only one child and one parent class and none other related class.
8. Which concept will result in derived class with more features (consider maximum 3 classes)?
a) Single inheritance
b) Multiple inheritance
c) Multilevel inheritance
d) Hierarchical inheritance
View Answer
Explanation: If single inheritance is used then only feature of a single class are inherited, and if multilevel inheritance is used, the 2nd class might have use private inheritance. Hence only multiple inheritance can result in derived class with more features. This is not mandatory but in a case if we consider same number of features in each class, it will result the same.
9. Single level inheritance is safer than _____________
a) Multiple inheritance
b) Interfaces
c) Implementations
d) Extensions
View Answer
Explanation: Interfaces also represent a way of inheritance but is a wide word to decide which inheritance we are talking about in it, hence can’t be considered. Implementation and extensions also doesn’t match that level of specific idea. And multiple inheritance not so safe as it might result in some ambiguity.
10. Which language doesn’t support single level inheritance?
a) Java
b) C++
c) Kotlin
d) All languages support it
View Answer
Explanation: All the languages support single level inheritance. Since any class can inherit other classes as required, if single level inheritance was not allowed it would result in failing a lot of features of OOP.
11. What is the output of the following program?
class A { protected: int a,b; public: void disp() { cout<<a<<b; } }; class B:public A { int x,y; };
a) Garbage value
b) Compile time error
c) Runtime error
d) Runs but gives random values as output
View Answer
Explanation: The compiler doesn’t find the main function and hence will throw an error main() missing. This program is using single level inheritance but the program is incomplete. Every program must implement main function.
12. What is the output of the following program?
class A { float sal=40000; } class B extends A { int salBonus=10000; public static void main(String args[]) { B p=new B(); System.out.println("B salary is:"+p.sal); System.out.println("Bonus of B is:"+p.bonus); } }
a)
B salary is: 4000.0 Bonus of B is: 10000
b)
B salary is 10000 Bonus of B is: 4000.0
c) Compile time error
d) Runtime error
View Answer
Explanation: The program gives output as in option a. The program have used single level inheritance and hence have access to the parent class methods and variables. This program simply prints the value from parent class and from the child class.
13. Single level inheritance will be best for___________
a) Inheriting a class which performs all the calculations
b) Inheriting a class which can print all the calculation results
c) Inheriting a class which can perform and print all calculations
d) Inheriting all the classes for different calculations
View Answer
Explanation: Inheriting a class which can perform the most common task will be more efficient. If class which can perform all the calculations is inherited then there won’t be any problem to print the result too. But if a class which can do the most common task for all the other tasks, it will make real use of inheritance.
14. Which constructor will be called first from the classes involved in single inheritance from object of derived class?
a) Base class constructor
b) Derived class constructor
c) Both class constructors at a time
d) Runtime error
View Answer
Explanation: The base class constructor will be called first from the object of derived class constructor. When the derived class members are to be initialized and allocated memory, the base class members should also be confirmed with the memory allocation.
15. If base class contains 2 nested classes, will it be possible to implement single level inheritance?
a) Yes, always
b) Yes, only if derived class also have nested classes
c) No, it will use more than 2 classes which is wrong
d) No, never
View Answer
Explanation: The nested classes are also members of a class. They behave as a used defined data type with some methods implementation. So the inheritance will be as usual with the nested classes being member of base class and of derived class if not private.
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.
- Practice Information Science MCQs
- Check Object Oriented Programming Books
- Practice MCA MCQs
- Practice Computer Science MCQs
- Apply for C++ Internship