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)- A. no del -2
- B. 3
- C. no del 0
- 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
- What is printed? python def acc(): total = 0 while True: try: x = yield total except ValueError: total = 0 continue if x is None: break…
- What is printed? python class Base: def init(self): self.log = ["Base"] class L(Base): def init(self): super().init() self.log.append("L")…
- What is printed? python class D: def get(self, obj, typ): return "desc" class DD(D): def set(self, obj, v): pass class C: a = D() b = DD()…
- What is printed? python class K: def init(self, v): self.v = v def hash(self): return hash(self.v) def eq(self, o): return self.v == o.v k…
- What is printed? python def f(x): try: r = 10 / x except ZeroDivisionError: r = "inf" return r else: r = round(r) finally: r = str(r) +…
- What is printed? python class A: def eq(self, o): print("A", end=" ") return NotImplemented class B: def eq(self, o): print("B", end=" ")…