Bird
0
0

Identify the error in this class definition:

medium📝 Debug Q14 of 15
Python - Constructors and Object Initialization
Identify the error in this class definition:
class Car:
    def __init__(self, model):
        model = model

my_car = Car("Tesla")
print(my_car.model)
AThe print statement should be print(model)
BThe constructor name is incorrect
CThe class is missing a return statement
DThe constructor does not assign model to self.model
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor assignment

    The constructor assigns model to local variable model, not to self.model.
  2. Step 2: Understand attribute access

    Without self.model, the object has no model attribute, causing an error on print.
  3. Final Answer:

    The constructor does not assign model to self.model -> Option D
  4. Quick Check:

    Use self.model = model to store attribute [OK]
Quick Trick: Always assign to self.attribute inside __init__ [OK]
Common Mistakes:
  • Assigning to local variable instead of self.attribute
  • Thinking constructor name is wrong
  • Expecting print(model) to work outside class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes