Bird
0
0

Find the error in this code:

medium📝 Debug Q6 of 15
Python - Object-Oriented Programming Foundations
Find the error in this code:
class Car:
    def __init__(self, model):
        model = model

c = Car('Tesla')
print(c.model)
AThe attribute is not assigned to self
BMissing parentheses in class definition
Cprint statement syntax is wrong
DClass name should be lowercase
Step-by-Step Solution
Solution:
  1. Step 1: Check attribute assignment in __init__

    The code assigns 'model' to local variable 'model', not to self.model.
  2. Step 2: Understand why print(c.model) fails

    Since self.model is never set, c.model does not exist, causing an error.
  3. Final Answer:

    The attribute is not assigned to self -> Option A
  4. Quick Check:

    Attribute assignment = self.attribute [OK]
Quick Trick: Always assign attributes with self.attribute = value [OK]
Common Mistakes:
  • Assigning to local variable instead of self.attribute
  • Forgetting to use self in methods
  • Assuming variable names auto become attributes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes