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__"))
  1. A. 2 True
  2. B. AE 2 True
  3. C. AE 2 False
  4. 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

Exams that ask this topic