Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Multiple Inheritance and Method Resolution
What will be the output of this code?
class A:
    def who(self):
        return 'A'
class B(A):
    def who(self):
        return 'B'
class C(A):
    def who(self):
        return 'C'
class D(B, C):
    pass
print(D().who())
AA
BB
CC
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand class hierarchy

    D inherits from B and C; B and C inherit from A. D does not override who().
  2. Step 2: Apply MRO to find who()

    MRO for D is D, B, C, A. So who() from B is called first.
  3. Final Answer:

    B -> Option B
  4. Quick Check:

    MRO lookup finds B's who() first [OK]
Quick Trick: MRO checks left to right in base classes list [OK]
Common Mistakes:
  • Choosing C's method instead of B's
  • Expecting error due to multiple inheritance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes