Bird
0
0

Identify the problem in this code:

medium📝 Debug Q7 of 15
Python - Multiple Inheritance and Method Resolution
Identify the problem in this code:
class A:
    def show(self):
        return 'A'

class B:
    def show(self):
        return 'B'

class C(A, B):
    pass

class D(B, A):
    pass

print(C().show())
print(D().show())
AOutput differs due to different inheritance order
BBoth print statements cause errors
CBoth print statements output 'A'
DBoth print statements output 'B'
Step-by-Step Solution
Solution:
  1. Step 1: Check inheritance order for C and D

    C inherits A then B; D inherits B then A.
  2. Step 2: Understand method resolution order effect

    C().show() calls A.show(), D().show() calls B.show() because of order.
  3. Final Answer:

    Output differs due to different inheritance order -> Option A
  4. Quick Check:

    Inheritance order affects method called [OK]
Quick Trick: Order of parents changes which method runs [OK]
Common Mistakes:
  • Assuming same output for both classes
  • Expecting errors due to multiple inheritance
  • Ignoring method resolution order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes