ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: D. True 1 None False

Reading d['a'] on a defaultdict inserts the default value, so the key exists afterwards. get() never inserts, so d.get('b') is None and 'b' is still absent.

More Python Programming questions

Exams that ask this topic