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 Animal:
    def sound(self):
        return "Some sound"

class Cat(Animal):
    def sound(self):
        return "Meow"

pet = Cat()
print(pet.sound())
AError
B"Some sound"
CNone
D"Meow"
Step-by-Step Solution
Solution:
  1. Step 1: Understand inheritance

    The class Cat inherits from Animal and overrides the sound method.
  2. Step 2: Method overriding

    When pet.sound() is called, Python uses the sound method defined in Cat, not Animal.
  3. Final Answer:

    "Meow" -> Option D
  4. Quick Check:

    Overridden method in child class is called [OK]
Quick Trick: Child class method overrides parent method [OK]
Common Mistakes:
MISTAKES
  • Assuming parent method is called instead of child's
  • Confusing method overriding with overloading
  • Expecting an error due to method name clash

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes