ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
class S:
    __slots__ = ("a",)
class T(S):
    pass
t = T()
t.a = 1
t.b = 2
s = S()
try:
    s.b = 2
except AttributeError:
    print("AE", end=" ")
print(hasattr(t, "__dict__"), hasattr(s, "__dict__"))
  1. A. AE True False
  2. B. AE False False
  3. C. True False
  4. D. AE True True

Correct answer: A. AE True False

__slots__ removes the instance __dict__ only if every class in the hierarchy defines slots. T does not declare __slots__, so its instances get a __dict__ and accept b; S instances still reject new attributes.

More Python Programming questions

Exams that ask this topic