C++ Programming Multiple Choice Questions - Inheritance
This section focuses on the "Inheritance" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. When the inheritance is private, the private methods in base class are __________ in the derived class (in C++).
A. Inaccessible
B. Accessible
C. Protected
D. Public
View Answer
Ans : A
Explanation: When the inheritance is private, the private methods in base class are inaccessible in the derived class (in C++).
2. Which design patterns benefit from the multiple inheritances?
A. Adapter and observer pattern
B. Code pattern
C. Glue pattern
D. None of the mentioned
View Answer
Ans : A
Explanation: A template is a formula for creating a generic class
3. What is meant by multiple inheritance?
A. Deriving a base class from derived class
B. Deriving a derived class from base class
C. Deriving a derived class from more than one base class
D. None of the mentioned
View Answer
Ans : C
Explanation: Multiple inheritance enables a derived class to inherit members from more than one parent.
4. What will be the order of execution of base class constructors in the following method of inheritance.class a: public b, public c {...};
A. b(); c(); a();
B. c(); b(); a();
C. a(); b(); c();
D. b(); a(); c();
View Answer
Ans : A
Explanation: b(); c(); a(); the order of execution of base class constructors in the following method of inheritance.class a: public b, public c {...};
5. Inheritance allow in C++ Program?
A. Class Re-usability
B. Creating a hierarchy of classes
C. Extendibility
D. All of the above
View Answer
Ans : D
Explanation: Advantage of inheritance are like re-usability- You can re-use existing class in a new class that avoid re-writing same code and efforts.We can make an application easily extensible.
6. Can we pass parameters to base class constructor though derived class or derived class constructor?
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: Yes, we pass parameters to base class constructor though derived class or derived class constructor.
7. What are the things are inherited from the base class?
A. Constructor and its destructor
B. Operator=() members
C. Friends
D. All of the above
View Answer
Ans : D
Explanation: These things can provide necessary information for the base class to make a logical decision.
8. Which of the following advantages we lose by using multiple inheritance?
A. Dynamic binding
B. Polymorphism
C. Both Dynamic binding & Polymorphism
D. None of the mentioned
View Answer
Ans : C
Explanation: The benefit of dynamic binding and polymorphism is that they help making the code easier to extend but by multiple inheritance it makes harder to track.
9. What will be the output of the following program?
Note:Includes all required header files
class find {
public:
void print() { cout <<" In find"; }
};
class course : public find {
public:
void print() { cout <<" In course"; }
};
class tech: public course { };
int main(void)
{
tech t;
t.print();
return 0;
}
A. In find
B. In course
C. Compiler Error: Ambiguous call to print()
D. None of the above
View Answer
Ans : B
Explanation: The print findction is not present in class tech. So it is looked up in the inheritance hierarchy. print() is present in both classes find and course, which of them should be called? The idea is, if there is multilevel inheritance, then findction is linearly searched up in the inheritance hierarchy until a matching findction is found.
10. Which symbol is used to create multiple inheritance?
A. Dot
B. Comma
C. Dollar
D. None of the above
View Answer
Ans : B
Explanation: For using multiple inheritance, simply specify each base class (just like in single inheritance), separated by a comma.
Also check :
Discussion