Welcome to our blog post, "C++ Inheritance MCQ Quiz - 20 MCQ Questions and Answers." This quiz is designed for everyone who wants to assess and improve their understanding of inheritance in C++, a fundamental concept in object-oriented programming. Whether you're a student gearing up for an exam, a programming enthusiast keen to test your knowledge or a professional brushing up on C++ basics, these 20 multiple-choice questions will help you gauge your grasp of inheritance mechanisms in C++.
Each question is followed by detailed answers to enhance your learning. So, challenge yourself and deepen your understanding of C++ inheritance! Let's dive in.
1. What is inheritance in C++?
Answer:
Explanation:
Inheritance in C++ allows for creating new classes (derived classes) that inherit properties and behaviors from existing classes (base classes), promoting code reusability and hierarchical relationships.
2. What keyword is used to inherit a class in C++?
Answer:
Explanation:
In C++, the colon (:) symbol is used in a class declaration to specify that the class inherits from another class.
3. Which type of inheritance in C++ involves multiple base classes for a single derived class?
Answer:
Explanation:
Multiple inheritance is a feature of C++ where a class (derived class) can inherit from more than one base class.
4. In C++, if a base class's destructor is not declared virtual, what is the impact?
Answer:
Explanation:
If a base class's destructor is not declared virtual, when deleting an object through a base class pointer, the derived class's destructor will not be called, which can lead to resource leaks or other issues.
5. What is the correct order of constructor calls in case of multilevel inheritance in C++?
Answer:
Explanation:
In multilevel inheritance, the constructor of the base class is called first, followed by the constructors of derived classes in the order of inheritance.
6. What is an abstract class in C++?
Answer:
Explanation:
An abstract class in C++ is a class that contains at least one pure virtual function. It cannot be instantiated and is intended to be a base class for other classes.
7. Which access specifier should be used for data members that should be accessible only within the class and its derived classes?
Answer:
Explanation:
The protected access specifier in C++ makes data members accessible within the class itself and its derived classes, but not from outside these classes.
8. What is 'diamond problem' in the context of inheritance in C++?
Answer:
Explanation:
The diamond problem occurs in C++ multiple inheritance when two or more base classes have a common base, leading to ambiguity.
9. Can constructor and destructor be inherited in C++?
Answer:
Explanation:
Constructors and destructors are not inherited in C++. Each derived class must define its own constructor and destructor.
10. Which inheritance type in C++ does not allow private and protected members of the base class to be accessed from the derived class?
Answer:
Explanation:
In private inheritance, members of the base class are inherited privately, meaning they cannot be accessed directly by the derived class.
11. What is the purpose of the 'virtual' keyword in inheritance in C++?
Answer:
Explanation:
The 'virtual' keyword in C++ is used to enable dynamic binding (or late binding) of functions, allowing function overriding in derived classes.
12. What is the impact of making a function 'virtual' in a base class?
Answer:
Explanation:
Declaring a function as 'virtual' in a base class allows it to be overridden in derived classes, providing customized behavior in each derived class.
13. What is function overriding in C++?
Answer:
Explanation:
Function overriding in C++ occurs when a derived class provides a specific implementation of a function that is already defined in its base class.
14. What happens when a derived class object is assigned to a base class object?
Answer:
Explanation:
Object slicing happens when a derived class object is assigned to a base class object, leading to the loss of the derived part of the object.
15. What is meant by 'upcasting' in C++?
Answer:
Explanation:
Upcasting in C++ refers to converting a derived class object to a base class object. It is a safe operation as every derived class object is also a base class object.
16. In which type of inheritance are all public members of the base class inherited as private members in the derived class?
Answer:
Explanation:
In private inheritance, all public and protected members of the base class are inherited as private members in the derived class.
17. What is a virtual destructor in C++?
Answer:
Explanation:
A virtual destructor in C++ ensures that the destructor of the derived class is called when an object is deleted through a base class pointer. It is crucial for proper resource cleanup in inheritance hierarchies.
18. Which feature of C++ supports the 'Open-Closed' principle in OOPs?
Answer:
Explanation:
Inheritance in C++ supports the 'Open-Closed' principle, which states that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
19. What is the main advantage of using protected access specifier in base classes?
Answer:
Explanation:
The protected access specifier allows members of a base class to be accessible in derived classes while keeping them inaccessible from outside the classes.
20. What is method hiding in C++?
Answer:
Explanation:
Method hiding occurs in C++ when a method in a derived class has the same name as a method in the base class but does not override it (e.g., different parameters). This hides the base class method.
Comments
Post a Comment
Leave Comment