Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Inheritance and Code Reuse
What will be the output of this code?
class Animal:
    def sound(self):
        print('Animal sound')

class Dog(Animal):
    def sound(self):
        super().sound()
        print('Bark')

d = Dog()
d.sound()
ABark
BBark\nAnimal sound
CAnimal sound
DAnimal sound\nBark
Step-by-Step Solution
Solution:
  1. Step 1: Understand method calls

    The Dog.sound() method first calls super().sound(), which runs Animal.sound().
  2. Step 2: Output order

    It prints 'Animal sound' first, then prints 'Bark'.
  3. Final Answer:

    Animal sound\nBark -> Option D
  4. Quick Check:

    super() calls parent method before child's print [OK]
Quick Trick: super() calls parent method first, then child's code runs [OK]
Common Mistakes:
  • Assuming child's print runs before parent's
  • Ignoring super() call
  • Confusing output order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes