ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
class P:
__slots__ = ("x",)
def __init__(self):
self.x = 1
p = P()
try:
p.y = 2
except AttributeError:
print("AE", end=" ")
p.x += 1
print(p.x, hasattr(p, "__dict__"))- A. 2 True
- B. AE 2 True
- C. AE 2 False
- D. AE 1 False
Correct answer: C. AE 2 False
__slots__ = ("x",) replaces the per-instance __dict__ with a fixed slot for x. Assigning any other attribute raises AttributeError, so "AE" is printed. The slot x can still be read and updated normally (1 becomes 2), and instances have no __dict__ at all.
More Python Programming questions
- What is printed? python 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…
- 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) +…