Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Python - Inheritance and Code Reuse
What will be the output of this code?
class Parent:
    def greet(self):
        return "Hello from Parent"

class Child(Parent):
    pass

obj = Child()
print(obj.greet())
AHello from Parent
BError: greet method not found
CHello from Child
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Check if Child overrides greet

    Child class uses pass, so it does not override greet method.
  2. Step 2: Calling greet on Child object

    Since Child inherits Parent, it uses Parent's greet method, returning "Hello from Parent".
  3. Final Answer:

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

    Child inherits parent's method if not overridden [OK]
Quick Trick: If child has no method, parent's method is used [OK]
Common Mistakes:
  • Expecting child's own greet output
  • Thinking pass means no methods inherited
  • Confusing pass with method override

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes