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")
AWrong class name
BMissing <code>self</code> parameter in <code>__init__</code>
CMissing parentheses when creating object
DNo error
Step-by-Step Solution
Solution:
  1. Step 1: Check __init__ method parameters

    The __init__ method must have self as the first parameter, but it is missing here.
  2. Step 2: Identify impact of missing self

    Without self, self.name causes an error because self is undefined.
  3. Final Answer:

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

    __init__ needs self first [OK]
Quick Trick: Always put self as first __init__ parameter [OK]
Common Mistakes:
  • Forgetting self in method parameters
  • Thinking class name must change
  • Missing parentheses when creating object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes