ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
d = {}
try:
    d[[1]] = 2
except TypeError as e:
    print(type(e).__name__)
print(d)
  1. A. KeyError | {}
  2. B. None | {}
  3. C. TypeError | {[2]: 9}
  4. D. TypeError | {}

Correct answer: D. TypeError | {}

Lists are unhashable, so they cannot be dict keys, and the assignment raises TypeError before the dict changes. The dict stays empty.

More Python Programming questions

Exams that ask this topic