ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
s = set()
s.add((1, 2))
s.add((2, 1))
try:
    s.add([1])
except TypeError:
    s.add(frozenset([1]))
s.add(frozenset({1}))
print(len(s), (1, 2) in s, {1} in s)
  1. A. 4 True False
  2. B. 3 True True
  3. C. 2 True True
  4. D. 3 True False

Correct answer: B. 3 True True

Tuples are ordered, so (1, 2) and (2, 1) are different. Lists are unhashable, so a frozenset is added instead; the second equal frozenset is a duplicate. A set can be looked up in a set via its frozenset equivalent.

More Python Programming questions

Exams that ask this topic