Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Python - Multiple Inheritance and Method Resolution

What will be the output of this code?

class X:
    def who(self):
        print('X')

class Y(X):
    def who(self):
        print('Y')

class Z(X):
    def who(self):
        print('Z')

class W(Y, Z):
    def who(self):
        super().who()

w = W()
w.who()
AY
BError: super() call ambiguous
CX
DZ
Step-by-Step Solution
Solution:
  1. Step 1: Analyze class W's who() method

    W overrides who() and calls super().who(), which calls the next method in MRO.
  2. Step 2: Determine MRO and next method

    MRO for W is W -> Y -> Z -> X. super().who() in W calls Z's who(), which prints 'Z'.
  3. Final Answer:

    Z -> Option D
  4. Quick Check:

    super() calls next in MRO = Z's who() [OK]
Quick Trick: super() calls next method in MRO order [OK]
Common Mistakes:
  • Expecting Z or X to print instead of Y
  • Thinking super() is ambiguous here
  • Ignoring MRO order in super() calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes