ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
class T:
    def __init__(self):
        self._c = 0
    @property
    def c(self):
        return self._c
    @c.setter
    def c(self, v):
        self._c = max(0, v)

t = T()
t.c = -5
t.c += 3
try:
    del t.c
except AttributeError:
    print("no del", end=" ")
print(t.c)
  1. A. no del -2
  2. B. 3
  3. C. no del 0
  4. D. no del 3

Correct answer: D. no del 3

t.c = -5 goes through the setter and stores max(0, -5) = 0. t.c += 3 reads the property (0), adds 3 and writes 3 back through the setter. The property defines no deleter, so del t.c raises AttributeError. The -2 option ignores the clamp done on the first assignment.

More Python Programming questions

Exams that ask this topic