Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q13 of Q15
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

c = Child()
print(c.greet())
AHello from Child
BHello from Parent
CAttributeError
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand method inheritance

    Child class inherits greet method from Parent because it has no own greet.
  2. Step 2: Trace the method call

    Calling c.greet() runs Parent's greet returning "Hello from Parent".
  3. Final Answer:

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

    Inherited method runs if child has none [OK]
Quick Trick: Child uses parent's method if not overridden [OK]
Common Mistakes:
MISTAKES
  • Expecting child's own greet method when none exists
  • Confusing AttributeError with missing method
  • Thinking syntax error occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes