Bird
0
0

Identify the bug in this code snippet:

medium📝 Debug Q7 of 15
Python - Multiple Inheritance and Method Resolution

Identify the bug in this code snippet:

class A:
    def greet(self):
        print('A')

class B(A):
    def greet(self):
        super().greet()
        print('B')

class C(A):
    def greet(self):
        super().greet()
        print('C')

class D(B, C):
    def greet(self):
        super().greet()
        print('D')

d = D()
d.greet()
AMissing explicit calls to parent classes cause errors.
Bsuper() calls cause infinite recursion.
CClass D should not inherit from both B and C.
DNo bug; output is correct and respects MRO.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze super() calls in each greet()

    Each greet() calls super().greet(), following MRO and printing messages in order.
  2. Step 2: Confirm output and correctness

    Output will be A, C, B, D respecting MRO: D -> B -> C -> A. No recursion or errors occur.
  3. Final Answer:

    No bug; output is correct and respects MRO. -> Option D
  4. Quick Check:

    super() chain works correctly in diamond inheritance [OK]
Quick Trick: Use super() in all classes to avoid diamond bugs [OK]
Common Mistakes:
  • Thinking super() causes recursion here
  • Expecting errors without explicit parent calls
  • Believing multiple inheritance is disallowed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes