Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
Python - Multiple Inheritance and Method Resolution
What will be the output of the following code?
class Parent1:
    def message(self):
        return "Message from Parent1"

class Parent2:
    def message(self):
        return "Message from Parent2"

class Child(Parent2, Parent1):
    pass

obj = Child()
print(obj.message())
AMessage from Parent2
BMessage from Parent1
CError: Ambiguous method call
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Understand inheritance order

    The class Child inherits from Parent2 first, then Parent1.
  2. Step 2: Method Resolution Order (MRO)

    Python looks for the message method in Child, then Parent2, then Parent1.
  3. Final Answer:

    Message from Parent2 -> Option A
  4. Quick Check:

    Check inheritance order and method lookup [OK]
Quick Trick: MRO follows left-to-right inheritance order [OK]
Common Mistakes:
  • Assuming Parent1's method is called first
  • Thinking Python raises ambiguity error
  • Ignoring the order of base classes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes