C++ Inheritance MCQ Quiz - MCQ Questions and Answers

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++?

a) A way to create new classes from existing classes
b) Copying the behavior of one function to another
c) Sharing variables between classes
d) A technique for data hiding

Answer:

a) A way to create new classes from existing classes

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++?

a) extends
b) inherits
c) derives
d) :

Answer:

d) :

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?

a) Single inheritance
b) Multiple inheritance
c) Multilevel inheritance
d) Hierarchical inheritance

Answer:

b) Multiple inheritance

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?

a) Derived class destructor won't be called
b) Base class destructor won't be called
c) Memory leak
d) Compiler error

Answer:

a) Derived class destructor won't be called

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++?

a) Derived class, then base class
b) Base class, then derived class
c) Random order
d) Simultaneous

Answer:

b) Base class, then derived class

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++?

a) A class that has at least one pure virtual function
b) A class that cannot be instantiated
c) A class that has only private members
d) A class designed only for inheritance

Answer:

a) A class that has at least one pure virtual function

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?

a) Public
b) Private
c) Protected
d) Internal

Answer:

c) Protected

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++?

a) Problem with destructors in multiple inheritance
b) Ambiguity arising due to multiple inheritance
c) Syntax error when using multiple inheritance
d) Memory leak in multiple inheritance

Answer:

b) Ambiguity arising due to multiple inheritance

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++?

a) Yes
b) No
c) Only constructor can be inherited
d) Only destructor can be inherited

Answer:

b) No

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?

a) Public inheritance
b) Private inheritance
c) Protected inheritance
d) Friend inheritance

Answer:

b) Private inheritance

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++?

a) To prevent a class from being inherited
b) To allow dynamic binding of functions
c) To declare virtual classes
d) To create virtual functions only in base classes

Answer:

b) To allow dynamic binding of functions

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?

a) It can't be overridden in derived classes
b) It must be defined in the base class
c) It allows derived classes to provide their own implementation
d) It becomes a pure virtual function

Answer:

c) It allows derived classes to provide their own implementation

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++?

a) Changing the behavior of an operator
b) Defining a function in the derived class that is already defined in the base class
c) Using a function multiple times
d) Declaring functions outside a class

Answer:

b) Defining a function in the derived class that is already defined in the base class

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?

a) It leads to a compiler error
b) Only the base class part of the derived object is assigned
c) The derived class object is sliced to fit the base class object
d) The entire derived class object is copied

Answer:

c) The derived class object is sliced to fit the base class object

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++?

a) Converting a base class object to a derived class object
b) Converting a derived class object to a base class object
c) Increasing the size of an object
d) Casting a class to its superclass

Answer:

b) Converting a derived class object to a base class object

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?

a) Public inheritance
b) Private inheritance
c) Protected inheritance
d) Friend inheritance

Answer:

b) Private inheritance

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++?

a) A destructor that can be overridden in derived classes
b) A destructor that is automatically called when an object is deleted
c) A destructor that deallocates memory
d) A destructor that is used only in virtual classes

Answer:

a) A destructor that can be overridden in derived classes

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?

a) Function overloading
b) Operator overloading
c) Inheritance
d) Templates

Answer:

c) Inheritance

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?

a) It makes the class abstract
b) It allows derived classes to access the members, but prevents access from other classes
c) It hides the data from derived classes
d) It is similar to private, but more secure

Answer:

b) It allows derived classes to access the members, but prevents access from other classes

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++?

a) Hiding the implementation of a method from the user
b) A derived class method hides a method of the base class with the same name
c) Making a method private
d) Overloading a method in the derived class

Answer:

b) A derived class method hides a method of the base class with the same name

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