Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q5 of Q15
Python - Polymorphism and Dynamic Behavior
What will be the output of this code?
class Parent:
    def message(self):
        print('Message from Parent')

class Child(Parent):
    def message(self):
        super().message()
        print('Message from Child')

c = Child()
c.message()
AMessage from Child\nMessage from Parent
BMessage from Parent\nMessage from Child
CMessage from Child
DMessage from Parent
Step-by-Step Solution
Solution:
  1. Step 1: Call to super()

    The message method in Child calls super().message(), which invokes Parent's method first.
  2. Step 2: Subsequent print

    After the parent's message prints, Child's own print statement runs.
  3. Final Answer:

    Message from Parent\nMessage from Child -> Option B
  4. Quick Check:

    super() calls parent method first [OK]
Quick Trick: super() calls parent method before child code [OK]
Common Mistakes:
MISTAKES
  • Assuming child message prints first
  • Expecting only one message printed
  • Ignoring super() call effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes