Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q5 of Q15
Python - Multiple Inheritance and Method Resolution
What will be the output of this code?
class X:
    def who(self):
        return "X"

class Y:
    def who(self):
        return "Y"

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

z = Z()
print(z.who())
AY
BX
CZ
DError: super() call invalid
Step-by-Step Solution
Solution:
  1. Step 1: Understand super() in multiple inheritance

    In class Z, super().who() calls the next method in MRO after Z.
  2. Step 2: Check MRO order

    Z's MRO is Z, X, Y, object. So super() calls X's who method.
  3. Final Answer:

    X -> Option B
  4. Quick Check:

    super() calls next in MRO = X [OK]
Quick Trick: super() calls next method in MRO chain [OK]
Common Mistakes:
MISTAKES
  • Thinking super() calls method from second parent
  • Expecting child's own method to be called
  • Assuming super() causes error here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes