C++ MCQs with Explanation (Inheritance)

1. The concept of inheritance enables C++

Explanation ≡ Correct answer: option b) to create new classes from existing classes
Inheritance in C++ allows a class to inherit properties and behaviors (data members and member functions) from an existing class. The derived class is created from the existing class (base class) and can extend or modify its functionality. This allows code reusability and enables the creation of new classes based on existing ones.

2. What is the advantage of using inheritance in C++?

Explanation ≡ Correct answer: options a), b), and c) - Facilitates creation of class libraries, Enables reuse of existing code, and Enhances reliability of the code
Inheritance allows us to create class hierarchies, facilitating the creation of class libraries with related classes. It promotes code reusability, as classes can inherit common properties and behaviors from a base class. By using inheritance, we can extend and modify existing classes, improving the reliability of the code by reusing well-tested components.

3. Through inheritance, it is possible to inherit from a class

Explanation ≡ Correct answer: option c) any members as we desire
Through inheritance, a derived class can inherit any members (data members and member functions) of the base class. It can inherit data members, member functions, constructors, and even functions declared as friend. The access specifiers (private, protected, public) determine the visibility of inherited members in the derived class.

4. In the class definition

class M : visibility-mode N { members of M };

the visibility-mode

Explanation ≡ Correct answer: option d) may be blank, public or private
In the class definition, the visibility mode (public, private, protected) before the base class name 'N' determines the access level of the inherited members in the derived class 'M'. It can be left blank (defaulting to private) or explicitly specified as public or private based on the desired access level. For example, using 'public' will make the inherited members public in the derived class, while using 'private' will make them private.

5. Consider the code segment

class A { private: int a; public: int b; protected: int c; }; class B : A { }; class C : public B { };

The derived class C would contain the members

Explanation ≡ Correct answer: option b) b and c only
In the given code, class B inherits the members of class A (private, public, and protected). When class C inherits from class B using the 'public' keyword, it can access the public and protected members of class B. So, class C would contain the members 'b' and 'c', but not 'a' since 'a' is a private member and cannot be accessed by derived classes.

6. Which of the following statements is TRUE, when a derived class D inherits a base class B using protected specifier?

Explanation ≡ Correct answer: option a) public members of B become protected members of D
When a derived class inherits a base class using the 'protected' access specifier, the public members of the base class become protected members of the derived class. The protected members of the base class remain protected, and private members of the base class are not accessible by the derived class. This provides a level of encapsulation by restricting the visibility of inherited members outside the derived class.

7. Which of the following functions can have access to the protected data of a class?

Explanation ≡ Correct answer: options a), b), and c) - A function that is a friend of the class, A member function of a class declared a friend of the class, and A member function of a derived class
Protected members of a class are accessible by functions that are declared as friends of the class. Additionally, if a member function of a class is declared a friend of the class, it can also access the protected members. Moreover, a member function of a derived class (inherited from a base class) can also access the protected members of the base class. This allows for controlled access to the protected data within the class hierarchy.

8. The use of the scope resolution operator in inheritance enables us

Explanation ≡ Correct answer: option b) to determine the base class of a derived class
The scope resolution operator (::) in C++ is used to access the members of a base class in the derived class. It allows us to specify the base class from which a particular member is inherited. By using the scope resolution operator, we can disambiguate member names when there is a naming conflict between the base class and the derived class. It helps determine the base class of a derived class in case of multiple inheritance.

9. The process of a class inheriting attributes from several classes is known as

Explanation ≡ Correct answer: option b) multiple inheritance
Multiple inheritance is a feature of C++ that allows a class to inherit attributes (data members and member functions) from more than one base class. This enables a class to combine features and behaviors from multiple sources, creating complex class hierarchies. In multiple inheritance, a class can inherit from two or more classes, combining their attributes to form the derived class.

10. A class D is privately derived from class B. An object of D in the main function can access

Explanation ≡ Correct answer: option c) protected members of B
When a class is privately derived from another class, all members of the base class become private in the derived class. Private members are not directly accessible outside the class. However, protected members of the base class become protected in the derived class, allowing the derived class and its derived classes to access those members. As a result, an object of D (privately derived from B) in the main function can access the protected members of B.

11. The following examples show that the class C is derived from classes A and B. Which one of them is legal?

Explanation ≡ Correct answer: options c) class C : public A : public B and e) class C: private A, public B;
The correct syntax for specifying multiple inheritance is to use a comma to separate the base classes. In option c) and e), the correct format is used to derive class C from classes A and B. The visibility mode (public, protected, private) before each base class name determines the access level of the inherited members in class C. 'public' means that public members of A and B will be public in class C, 'protected' means they will be protected, and 'private' means they will be private.

12. Consider the following code:

class A { }; class B : A { };

What happens when we compile this code?

Explanation ≡ Correct answer: option d) Will compile successfully
The code will compile successfully because class A and class B are defined properly. If a class does not have any members (data members or member functions), it is considered an empty class, and C++ allows the declaration of empty classes. Class B is derived from class A using public inheritance, so it will inherit the properties and behaviors of class A. Even though the body of class A is empty, the code is still valid and will compile without errors.

13. Which one of the following statements is FALSE?

Explanation ≡ Correct answer: option a) Deriving one class from another requires fundamental changes to the base class
The statement is FALSE. Deriving one class from another does not require any fundamental changes to the base class. The derived class inherits the properties and behaviors of the base class as they are, and modifications or extensions can be made in the derived class. Inheritance allows code reuse and promotes a hierarchical structure of classes without altering the base class. It provides the concept of "is-a" relationship, where a derived class is a type of the base class.

14. Consider the following class definition with inheritance:

class A : public B, virtual public C { };

The order of execution of constructors will be:

Explanation ≡ Correct answer: option d) A ( ), C ( ), B ( )
The order of execution of constructors is determined by the order of inheritance and the use of the 'virtual' keyword. When a class is virtually inherited (using 'virtual' keyword), its constructor is called before the constructors of non-virtually inherited classes. In this case, class A is virtually inherited from class C (virtual public C), so the constructor of class C is called first. Then, class A is publicly inherited from class B (public B), so the constructor of class B is called next. Finally, the constructor of class A is called. Therefore, the order of execution of constructors is A ( ), C ( ), B ( ).

15. Consider the following class:

class ABC { int a; int b; public : constructor function };

We wish to define the constructor function using an initialization list. Which one of the following is the legal constructor function?

Explanation ≡ Correct answer: options a) ABC (int x, int y): a (x), b(y) { } and d) All the above
Option a) is the legal constructor function using an initialization list. The constructor of class ABC takes two integer parameters x and y, and it initializes the data members 'a' and 'b' with the values of x and y, respectively. This is done using an initialization list, which is the proper way to initialize data members in the constructor. Option b) is not a legal constructor function because the initialization of 'a' in the initialization list relies on the value of 'y', which is not a valid approach for initializing members. Option c) is also not a legal constructor function because it initializes 'b' using the value of 'a' multiplied by 'y', which may not be desired and can lead to unexpected results. Option d) states that all the above options are correct, but option b) and c) are not legal constructor functions. Therefore, only option a) is a legal constructor function using an initialization list.


Your Score is 10

THE STUDENT FRIENDLY BOOK TO LEARN C++. THE EXPLANATIONS ARE IN SIMPLE ENGLISH AND THEY PROVIDE EXERCISES TO MAKE YOUR LEARNING EXCELLENT.

Topic wise C++ MCQs Index ≡
Click on particular topic to visit that page
  1. Principles of Object-Oriented Programming 
  2. Beginning with C++
  3. Tokens, Expressions, and Control Structures
  4. Functions in C++
  5. Classes and Objects
  6. Constructors and Destructors
  7. Operator Overloading
  8. Inheritance
  9. Pointers, Virtual Functions, and Polymorphism
  10. Managing Console I/O Operations
  11. Working with Files
  12. Templates
  13. Exception Handling
  14. Standard Template Library
  15. Manipulating Strings
  16. New Features in ANSI C++ Standards
  17. Object-Oriented System Development

Post a Comment

0 Comments