Bird
Raised Fist0

Find the bug in this code:

medium📝 Debug Q7 of Q15
Python - Polymorphism and Dynamic Behavior
Find the bug in this code:
class Car:
    def drive(self):
        print('Driving')

def operate(vehicle):
    vehicle.drive()

operate(Car())
operate('bike')
ASecond call causes AttributeError
BSyntaxError due to missing parentheses
CTypeError because 'bike' is not a Car
DNo error, both calls succeed
Step-by-Step Solution
Solution:
  1. Step 1: Check method calls

    First call passes Car instance with drive method, so it works.
  2. Step 2: Analyze second call

    Second call passes string 'bike' which lacks drive method, causing AttributeError.
  3. Final Answer:

    Second call causes AttributeError -> Option A
  4. Quick Check:

    Calling missing method causes AttributeError [OK]
Quick Trick: Passing wrong type without method causes AttributeError [OK]
Common Mistakes:
MISTAKES
  • Confusing AttributeError with TypeError
  • Expecting implicit method on string
  • Ignoring error on second call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes