Bird
0
0

Find the error in this class definition:

medium📝 Debug Q14 of 15
Python - Classes and Object Lifecycle
Find the error in this class definition:
class Person:
    def __init__(name):
        self.name = name

p = Person("Alice")
Aself.name should be name.name
BMissing colon after class name
CIncorrect object creation syntax
DMissing self parameter in __init__ method
Step-by-Step Solution
Solution:
  1. Step 1: Check __init__ method parameters

    The first parameter of instance methods must be self. Here, __init__ lacks self.
  2. Step 2: Confirm other syntax correctness

    Class header has colon, object creation syntax is correct, and self.name assignment is proper.
  3. Final Answer:

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

    Instance methods need self as first parameter [OK]
Quick Trick: Always include self as first method parameter [OK]
Common Mistakes:
  • Omitting self in methods
  • Forgetting colon after class name
  • Misusing self in attribute assignment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes