Bird
Raised Fist0

Find the error in this code snippet:

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

    def display(self):
        print(Model)
AConstructor name is wrong
BMissing self in display method
CModel should be self.model in print
DClass name should be lowercase
Step-by-Step Solution
Solution:
  1. Step 1: Check the print statement inside display method

    The print statement uses Model which is undefined; it should use self.model to access the instance variable.
  2. Step 2: Verify other parts

    The constructor name __init__ is correct, and method has self parameter. Class name capitalization is fine.
  3. Final Answer:

    Model should be self.model in print -> Option C
  4. Quick Check:

    Use self to access instance variables [OK]
Quick Trick: Use self.variable to access instance data [OK]
Common Mistakes:
MISTAKES
  • Forgetting self in method parameters
  • Using variable name without self prefix
  • Thinking constructor name is incorrect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes