Bird
0
0

Consider the following code snippet. What is the error and how to fix it?

medium📝 Debug Q14 of 15
Python - Multiple Inheritance and Method Resolution
Consider the following code snippet. What is the error and how to fix it?
class X:
    def method(self):
        return 'X'

class Y:
    def method(self):
        return 'Y'

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

print(Z().method())
AError: super() call is ambiguous; fix by specifying class and self
BOutput: 'X' (no error)
COutput: 'Y' (no error)
DError: Missing parentheses in print statement
Step-by-Step Solution
Solution:
  1. Step 1: Analyze super() in Z.method()

    super() calls next method in MRO after Z, which is X.method().
  2. Step 2: Check output of X.method()

    X.method() returns 'X', so print outputs 'X' with no error.
  3. Final Answer:

    Output: 'X' (no error) -> Option B
  4. Quick Check:

    super() calls next in MRO, here X.method() [OK]
Quick Trick: super() calls next method in MRO automatically [OK]
Common Mistakes:
  • Thinking super() needs explicit class and self
  • Expecting output 'Y' instead of 'X'
  • Assuming syntax error in print statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes