Bird
0
0

Find the error in this code:

medium📝 Debug Q14 of 15
Python - Methods and Behavior Definition
Find the error in this code:
class Car:
    def __init__(self, model):
        self.model = model
    def show_model():
        print(f"Model: {self.model}")

car = Car('Tesla')
car.show_model()
ACannot create object without arguments
BWrong attribute name used
CMissing self parameter in show_model method
Dprint statement syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Check method definition

    The method show_model is missing the self parameter, so it cannot access instance attributes.
  2. Step 2: Understand the error cause

    Calling car.show_model() passes the object automatically, but method lacks self to receive it, causing a TypeError.
  3. Final Answer:

    Missing self parameter in show_model method -> Option C
  4. Quick Check:

    Instance methods need self parameter [OK]
Quick Trick: Always include self as first parameter in instance methods [OK]
Common Mistakes:
  • Forgetting self in method definition
  • Trying to access self without parameter
  • Confusing class and instance methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes