Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - Multiple Inheritance and Method Resolution
What will be the output of this code?
class A:
    def greet(self):
        return "Hello from A"

class B:
    def greet(self):
        return "Hello from B"

class C(A, B):
    pass

obj = C()
print(obj.greet())
A"Hello from A"
BTypeError
CAttributeError
D"Hello from B"
Step-by-Step Solution
Solution:
  1. Step 1: Understand method resolution order (MRO)

    Class C inherits from A first, then B. Python looks for methods in the order of parents listed.
  2. Step 2: Determine which greet() is called

    Since A is first, C uses A's greet method, returning "Hello from A".
  3. Final Answer:

    "Hello from A" -> Option A
  4. Quick Check:

    MRO follows parent order = "Hello from A" [OK]
Quick Trick: First parent class method is used in multiple inheritance [OK]
Common Mistakes:
  • Assuming last parent class method is called
  • Expecting an error due to multiple parents
  • Confusing method names or forgetting MRO

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes