Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q4 of Q15
Python - Inheritance and Code Reuse

What will be the output of this code?

class Parent:
    def greet(self):
        print('Hello from Parent')

class Child(Parent):
    def greet(self):
        print('Hello from Child')

obj = Child()
obj.greet()
AHello from Parent
BHello from Child
CHello from Parent and Child
DError: greet method not found
Step-by-Step Solution
Solution:
  1. Step 1: Identify method overriding in Child class

    Child class defines its own greet method, replacing Parent's greet.
  2. Step 2: Calling greet on Child instance

    Calling obj.greet() uses Child's greet method, printing 'Hello from Child'.
  3. Final Answer:

    Hello from Child -> Option B
  4. Quick Check:

    Child method overrides parent method output [OK]
Quick Trick: Child method runs when overridden, not parent [OK]
Common Mistakes:
MISTAKES
  • Expecting parent method output
  • Thinking both messages print
  • Assuming method not found error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes