Bird
Raised Fist0

In a language that uses a linearized Method Resolution Order (MRO), if class X inherits from classes Y and Z, both defining a method bar(), which method is executed when bar() is called on an instance of X?

easy📖 Theory Q3 of Q15
OOP & Design Patterns - Inheritance - Types, Method Resolution Order & Diamond Problem
In a language that uses a linearized Method Resolution Order (MRO), if class X inherits from classes Y and Z, both defining a method bar(), which method is executed when bar() is called on an instance of X?
ABoth methods are called sequentially
BThe method from class <code>Z</code>, regardless of MRO
CThe method from class <code>Y</code>, if <code>Y</code> appears before <code>Z</code> in the MRO
DAn error occurs due to ambiguity
Step-by-Step Solution
Solution:
  1. Step 1: Understand MRO

    Linearized MRO defines a strict order in which base classes are searched for methods.
  2. Step 2: Identify method lookup

    When calling bar() on X, the method in the first base class found in the MRO is invoked.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Method called from first base class in MRO [OK]
Quick Trick: MRO calls method from first base class found [OK]
Common Mistakes:
MISTAKES
  • Assuming both methods are called
  • Believing the last base class method is called
  • Expecting an error due to ambiguity
Trap Explanation:
PITFALL
  • Option B looks plausible but ignores MRO order; Option C is incorrect as MRO calls only one method; Option D is wrong because MRO resolves ambiguity.
Interviewer Note:
CONTEXT
  • Tests understanding of how linearized MRO determines method invocation in multiple inheritance.
Master "Inheritance - Types, Method Resolution Order & Diamond Problem" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes