Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Polymorphism and Dynamic Behavior
What will be the output of this code?
class Bird:
    def sound(self):
        return "Chirp"

class Dog:
    def sound(self):
        return "Bark"

def animal_sound(animal):
    return animal.sound()

print(animal_sound(Bird()))
print(animal_sound(Dog()))
ABark\nChirp
BChirp\nBark
CError: sound method missing
DNone\nNone
Step-by-Step Solution
Solution:
  1. Step 1: Understand polymorphism in the code

    Both Bird and Dog classes have a sound() method. animal_sound calls sound() on any object passed.
  2. Step 2: Trace the print statements

    animal_sound(Bird()) returns "Chirp"; animal_sound(Dog()) returns "Bark"; prints are in that order.
  3. Final Answer:

    Chirp\nBark -> Option B
  4. Quick Check:

    Polymorphism output = Chirp then Bark [OK]
Quick Trick: Same method name, different classes, different outputs [OK]
Common Mistakes:
  • Assuming method names must be different
  • Expecting errors due to different classes
  • Confusing order of print statements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes