Bird
0
0

Identify the error in this code snippet using getter and setter methods:

medium📝 Debug Q14 of 15
Python - Encapsulation and Data Protection
Identify the error in this code snippet using getter and setter methods:
class Car:
    def __init__(self):
        self._speed = 0

    @property
    def speed(self):
        return self._speed

    @speed.setter
    def speed(self):
        self._speed = 100

c = Car()
c.speed = 50
print(c.speed)
ASetter method missing value parameter
BGetter method missing return statement
CProperty decorator used incorrectly
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check setter method signature

    Setter must accept two parameters: self and value to set the attribute.
  2. Step 2: Identify missing parameter

    Current setter only has self, missing value parameter, causing error on assignment.
  3. Final Answer:

    Setter method missing value parameter -> Option A
  4. Quick Check:

    Setter needs (self, value) parameters [OK]
Quick Trick: Setter must have value parameter besides self [OK]
Common Mistakes:
  • Omitting value parameter in setter
  • Confusing getter and setter decorators
  • Assuming code runs without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes