Inheritance in Action: Single vs Multiple Quiz Quiz

Explore key concepts of single and multiple inheritance in object-oriented programming with scenario-based questions. This quiz clarifies class relationships, method resolution, and potential pitfalls, guiding learners to identify how inheritance models impact code design and behavior.

  1. Understanding Single Inheritance

    In a scenario where class Dog inherits only from class Animal, which inheritance type best describes this relationship?

    1. Multiple inheritance
    2. Single inheritance
    3. Multilevel inheritance
    4. Hybrid inheritance

    Explanation: Single inheritance occurs when a class derives from only one parent, just like class Dog from class Animal. Multiple inheritance involves inheriting from more than one base class, which is not the case here. Multilevel inheritance refers to a chain (for example, Animal u003E Mammal u003E Dog), and hybrid inheritance combines more than one type of inheritance. Therefore, the relationship described is single inheritance.

  2. Multiple Inheritance in Practice

    If class FlyingFish inherits from both class Fish and class Flyer, which inheritance pattern does this represent?

    1. Multidimensional inheritance
    2. Single inheritance
    3. Multiple inheritance
    4. Hierarchical inheritance

    Explanation: Here, FlyingFish has two direct parents (Fish and Flyer), which is the defining feature of multiple inheritance. Single inheritance permits only one base class. Hierarchical inheritance is when multiple classes share a single parent, and multidimensional inheritance is not a standard term in object-oriented contexts. Only multiple inheritance accurately describes the relationship in the question.

  3. Method Resolution Order (MRO) Issue

    When both parent classes provide the same method and a subclass inherits from them, how does method resolution typically occur with multiple inheritance?

    1. The method from none of the parents is accessible
    2. The language-specific method resolution order determines which method runs
    3. Both methods execute simultaneously
    4. The method from the rightmost parent is always used

    Explanation: In multiple inheritance, the method resolution order (MRO) dictated by the programming language decides which parent's method is called. It is not universally from the rightmost parent, and methods do not usually execute at the same time. The last option is incorrect because the method is still accessible based on the MRO. Thus, the outcome depends on language-specific resolution rules.

  4. Ambiguity in Multiple Inheritance

    If two parent classes provide a method with the same name and a child class inherits from both, what common problem can arise?

    1. Name hiding
    2. Inheritance doubling
    3. Compilation success without errors
    4. Unresolved ambiguity

    Explanation: Unresolved ambiguity occurs when it is unclear which parent’s method should be inherited or run, typical in multiple inheritance scenarios. Name hiding is a different issue related to variable or method scopes. Compilation may actually fail or generate warnings in some languages, so successful compilation is not certain. 'Inheritance doubling' is not a recognized inheritance issue. Therefore, unresolved ambiguity is the main problem here.

  5. Selecting an Inheritance Model

    Given the need to create a class AmphibiousCar that shares behavior from both Car and Boat, which inheritance type allows directly inheriting from both classes?

    1. Duplicate inheritance
    2. Multiple inheritance
    3. Circular inheritance
    4. Single inheritance

    Explanation: Multiple inheritance allows a class to have more than one direct parent, such as inheriting features from both Car and Boat in the example. Single inheritance limits a class to one parent. Circular inheritance is problematic and generally not allowed, and duplicate inheritance is not a standard concept. Thus, multiple inheritance is the correct and applicable choice.