ProgrammingModerateTCS NQT · Wipro pattern

What is printed?

What is printed?
from collections import defaultdict
d = defaultdict(list)
d['a'].append(1)
print('b' in d, d['b'], len(d), 'b' in d)
  1. A. False [] 1 False
  2. B. True [] 2 True
  3. C. False [] 2 True
  4. D. False [] 2 False

Correct answer: C. False [] 2 True

The first membership test runs before 'b' is created, so it is False. Reading d['b'] creates the key with an empty list, so len is 2 and the last test is True.

More Python Programming questions

Exams that ask this topic