Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
Python - Polymorphism and Dynamic Behavior
What will be the output of the following code?
class Bird:
    def fly(self):
        print('Flying')

class Airplane:
    def fly(self):
        print('Jet flying')

objects = [Bird(), Airplane()]
for obj in objects:
    obj.fly()
AFlying\nJet flying
BJet flying\nFlying
CFlying\nFlying
DError: fly method not found
Step-by-Step Solution
Solution:
  1. Step 1: Understand the objects and their fly methods

    Bird has fly() printing 'Flying', Airplane has fly() printing 'Jet flying'.
  2. Step 2: Trace the loop calling fly on each object

    First object is Bird(), prints 'Flying'. Second is Airplane(), prints 'Jet flying'.
  3. Final Answer:

    Flying\nJet flying -> Option A
  4. Quick Check:

    Each object's fly() runs in order [OK]
Quick Trick: Each object's method runs in loop order [OK]
Common Mistakes:
  • Assuming type matters for method call
  • Mixing output order
  • Expecting error due to different classes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes