0% found this document useful (0 votes)
3 views5 pages

C++ Quizz Inheritance

The document discusses hierarchical inheritance, defining it as multiple derived classes inheriting from a single base class, and outlines key concepts such as member access, copies of base class members, and potential issues like the Diamond Problem in hybrid inheritance. It also addresses how to resolve ambiguities and the implications of virtual inheritance on object size. The document includes multiple-choice questions and answers to reinforce understanding of these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

C++ Quizz Inheritance

The document discusses hierarchical inheritance, defining it as multiple derived classes inheriting from a single base class, and outlines key concepts such as member access, copies of base class members, and potential issues like the Diamond Problem in hybrid inheritance. It also addresses how to resolve ambiguities and the implications of virtual inheritance on object size. The document includes multiple-choice questions and answers to reinforce understanding of these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Hierarchial inheritance :

What is the minimum number of classes required to implement hierarchical


inheritance?

• a) 1

• b) 2

• c) 3

• d) 4

2. Which statement best describes hierarchical inheritance?

• a) One derived class inherits from multiple base classes.

• b) Multiple derived classes inherit from a single base class.

• c) A class inherits from another derived class, forming a chain.

• d) Combining multiple inheritance and multilevel inheritance.

3. In hierarchical inheritance, if Class B and Class C both inherit from Class A,


which of the following is true?

• a) Class B can access private members of Class C.

• b) Class B and Class C both have their own copy of Class A's members.

• c) Class C inherits from Class B.

• d) Changes in Class B's data members will automatically update Class


C's members.

4. Which real-world scenario is an example of hierarchical inheritance?

• a) A 'SmartPhone' class inheriting from 'Camera' and 'Phone'.

• b) A 'Car' class inheriting from 'Vehicle', and 'SportsCar' inheriting from


'Car'.

• c) A 'SavingsAccount' and 'CurrentAccount' both inheriting from a


'BankAccount' class.

• d) A 'Child' class inheriting from 'Mother' and 'Father'.

[Link] a base class member is declared as protected, which classes can access it in a
hierarchical setup?
• a) Only the base class.

• b) Only the first derived class.

• c) All classes derived from that base class.

• d) Any class in the entire program

[Link] hierarchical inheritance require all derived classes to use the same access
specifier (e.g., all public)?

• a) Yes, they must match.

• b) No, each derived class can inherit the base class independently (one public,
one private, etc.).

• c) Only if the base class is abstract.

• d) Yes, but only for protected inheritance

[Link] of these is a potential disadvantage when hierarchical inheritance is


combined with multiple inheritance (Hybrid)?

• a) The "Circular Reference" problem.

• b) The "Diamond Problem" (ambiguity).

• c) Memory leaks in the base class.

• d) Reduced code reusability

[Link] many copies of base class members does each derived class receive?

• a) They all share one single copy.

• b) Each derived class gets its own unique, independent copy.

• c) Only the first class declared gets a copy.

• d) Copies are only made for static members.

1. c) At least 3 (One base and at least two derived)

2. b) Multiple derived classes inherit from a single base class

3. b) Class B and Class C both have their own copy of Class A's
members

4. c) A 'SavingsAccount' and 'CurrentAccount' both inheriting from


'BankAccount

5. c) All classes derived from that base class. protected members are
accessible to any child class Hero Vired.

6. b) No, each can inherit independently. Classes are independent; one can
use public while another uses private Sanfoundry.

7. b) The "Diamond Problem". This occurs when two derived classes (from
one base) are later inherited by a single 4th class Naukri Code 360.
8. b) Each derived class gets its own unique copy. Changes in Derived1 do
not affect Derived2 Sanfoundry

HYBRID INHERITANCE :
1. Which two types of inheritance are most commonly combined to create the
"Diamond Problem" in hybrid inheritance?

• a) Single and Multilevel

• b) Hierarchical and Multiple

• c) Hierarchical and Single

• d) Multiple and Multiple

[Link] is the primary issue caused by the Diamond Problem?

• a) Memory leak in the base class.

• b) The derived class receives multiple copies of the same base class members,
causing ambiguity.

• c) The program cannot have more than four classes.

• d) Constructors cannot be called for the base class

[Link] can you resolve member access ambiguity in a hybrid inheritance structure
without using virtual inheritance?

• a) By deleting the base class.

• b) Using the static keyword.

• c) Using the scope resolution operator (::) to specify the path.

• d) It cannot be resolved without virtual inheritance.

[Link] is the order of destructor calls in a hybrid inheritance hierarchy?

• a) Same as the order of constructor calls.


• b) Random order.

• c) Reverse order of the constructor calls.

• d) Only the derived class destructor is called

1. b) Hierarchical and Multiple. This creates a "diamond" shape where


two classes branch from one, and a final class merges them back.

2. b) Multiple copies/Ambiguity. The compiler doesn't know which path


to take to reach the shared base class members.
3. c) Scope resolution operator ( :: ). For
example, obj.Parent1::member tells the compiler exactly which path
to use.

4. c) Reverse order of constructor calls. Destructors always clean up


from the most derived class back up to the base class.
[Link] using virtual inheritance to solve the Diamond Problem, which class is
responsible for calling the virtual base class constructor?

• a) The immediate child classes.

• b) The most derived (bottom-most) class.

• c) The compiler does it automatically; you cannot call it.

• d) Only the first class in the declaration list

[Link] a Hybrid hierarchy, if a class inherits from two parents, and both parents have a
function with the same name, what happens if the child calls that function without
a scope resolution operator?

• a) It calls the function from the first parent listed.

• b) It calls the function from the second parent listed.

• c) A compile-time "ambiguity" error occurs.

• d) It calls the base-most version of the function

[Link] is the effect of virtual inheritance on the size of an object (using sizeof)?

• a) It decreases the size by sharing members.

• b) It usually increases the size due to the addition of a "virtual base pointer"
(vbptr).

• c) The size remains exactly the same.


• d) Size only increases if the base class has private members

5. b) The most derived class. In Virtual Inheritance, the most derived


class's constructor calls the virtual base constructor directly to avoid
multiple initializations GeeksforGeeks.

6. c) A compile-time "ambiguity" error. Even if the functions are


identical, the compiler needs you to specify the path Programiz.

7. b) It usually increases the size. The compiler adds a pointer (vbptr)


to keep track of the shared base class Microsoft Learn.

You might also like