Bird
0
0

Find the error in this class definition:

medium📝 Debug Q14 of 15
Python - Constructors and Object Initialization
Find the error in this class definition:
class Person:
    def __init__(name):
        self.name = name
p = Person('Alice')
print(p.name)
AUsing self before assignment
BIncorrect print statement
CMissing self parameter in __init__
DClass name should be lowercase
Step-by-Step Solution
Solution:
  1. Step 1: Check method parameters

    The __init__ method must have self as the first parameter to refer to the instance.
  2. Step 2: Identify the error

    Here, __init__ only has name, so self is missing, causing a runtime error.
  3. Final Answer:

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

    __init__ needs self first [OK]
Quick Trick: Always include self as first parameter in instance methods [OK]
Common Mistakes:
  • Forgetting self in __init__
  • Trying to use self without defining it
  • Assuming self is automatic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes