Bird
Raised Fist0

What will be the output of the following code?

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

What will be the output of the following code?

class Animal:
    def sound(self):
        print("Some sound")

class Dog(Animal):
    def sound(self):
        print("Bark")

pet = Dog()
pet.sound()
ASome sound
BNone
CError: Method not found
DBark
Step-by-Step Solution
Solution:
  1. Step 1: Identify method overriding in Dog class

    Dog class overrides the sound() method from Animal to print "Bark" instead of "Some sound".
  2. Step 2: Check which method is called

    When pet.sound() is called, it uses Dog's version, printing "Bark".
  3. Final Answer:

    Bark -> Option D
  4. Quick Check:

    Child method called = overridden output [OK]
Quick Trick: Child method replaces parent method output [OK]
Common Mistakes:
MISTAKES
  • Expecting parent method output instead of child's
  • Thinking method call causes error
  • Confusing print output with return value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes