Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Python - Polymorphism and Dynamic Behavior
What will be the output of this code?
class Vehicle:
    def move(self):
        return "Moving"

class Car(Vehicle):
    pass

v = Vehicle()
c = Car()
print(v.move())
print(c.move())
AError Moving
BMoving Error
CMoving Moving
DError Error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze class definitions

    Car inherits from Vehicle but does not override move method.
  2. Step 2: Check method calls

    Both v and c call move(), which is defined in Vehicle, so both return "Moving".
  3. Final Answer:

    Moving\nMoving -> Option C
  4. Quick Check:

    Inherited methods work if not overridden [OK]
Quick Trick: Child inherits parent methods if not overridden [OK]
Common Mistakes:
  • Expecting error when method is not overridden
  • Confusing inheritance with method absence
  • Assuming child must define all methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes