0
0
Pythonprogramming~20 mins

Property decorator usage in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Property Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of property getter
What is the output of this code?
Python
class Circle:
    def __init__(self, radius):
        self._radius = radius
    
    @property
    def radius(self):
        return self._radius

c = Circle(5)
print(c.radius)
AAttributeError
B5
CNone
DTypeError
Attempts:
2 left
💡 Hint
Look at how the property decorator allows access to the private variable.
Predict Output
intermediate
2:00remaining
Output after setting property value
What will be the output of this code?
Python
class Person:
    def __init__(self, name):
        self._name = name
    
    @property
    def name(self):
        return self._name
    
    @name.setter
    def name(self, value):
        self._name = value

p = Person('Alice')
p.name = 'Bob'
print(p.name)
AAlice
BAttributeError
CBob
DNone
Attempts:
2 left
💡 Hint
The setter changes the private variable _name.
Predict Output
advanced
2:00remaining
Output with property and deleter
What is the output of this code?
Python
class Data:
    def __init__(self):
        self._value = 10
    
    @property
    def value(self):
        return self._value
    
    @value.deleter
    def value(self):
        del self._value

d = Data()
print(d.value)
del d.value
print(hasattr(d, '_value'))
A
TypeError
True
B
10
True
C
AttributeError
False
D
10
False
Attempts:
2 left
💡 Hint
Deleting the property deletes the underlying attribute.
Predict Output
advanced
2:00remaining
Error when setting read-only property
What error does this code raise?
Python
class ReadOnly:
    @property
    def data(self):
        return 42

r = ReadOnly()
r.data = 10
AAttributeError
BTypeError
CSyntaxError
DValueError
Attempts:
2 left
💡 Hint
Properties without a setter are read-only.
🧠 Conceptual
expert
2:00remaining
Why use property decorators?
Which of the following is the main advantage of using property decorators in Python classes?
AThey allow methods to be accessed like attributes, enabling controlled access to private variables.
BThey automatically make all class variables public.
CThey replace the need for constructors in classes.
DThey speed up the execution of class methods significantly.
Attempts:
2 left
💡 Hint
Think about how properties help with encapsulation and attribute access.