Bird
0
0

Find the error in this class definition:

medium📝 Debug Q14 of 15
Python - Object-Oriented Programming Foundations
Find the error in this class definition:
class Person():
    def __init__(self, name):
        name = name

p = Person("Alice")
print(p.name)
AClass name should be lowercase
BMissing self in __init__ parameters
Cprint(p.name) should be print(name)
DShould assign to self.name, not name
Step-by-Step Solution
Solution:
  1. Step 1: Check attribute assignment in __init__

    The code assigns name = name, which only changes the local variable, not the object's attribute.
  2. Step 2: Correct assignment to object attribute

    It should be self.name = name to store the value in the object for later access.
  3. Final Answer:

    Should assign to self.name, not name -> Option D
  4. Quick Check:

    Use self.attribute = value to save data [OK]
Quick Trick: Assign attributes with self.attribute = value [OK]
Common Mistakes:
  • Assigning to local variable instead of self.attribute
  • Forgetting self in method parameters
  • Trying to print undefined variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes