Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 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):
    def greet(self):
        return super().greet() + " and Child"

c = Child()
print(c.greet())
AHello from Parent
BHello from Child
CHello from Parent and Child
DError: super() not used correctly
Step-by-Step Solution
Solution:
  1. Step 1: Understand method calls in Child.greet()

    Child's greet calls super().greet() which runs Parent's greet returning Hello from Parent.
  2. Step 2: Combine returned strings

    Child's greet adds and Child to the parent's string, so final output is Hello from Parent and Child.
  3. Final Answer:

    Hello from Parent and Child -> Option C
  4. Quick Check:

    super() calls parent method + extra text = A [OK]
Quick Trick: super() returns parent result; child can add more [OK]
Common Mistakes:
  • Expecting only parent's message without child addition
  • Thinking super() causes error here
  • Ignoring the string concatenation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes