ProgrammingHardCognizant GenC · TCS NQT

What is printed?

What is printed?
from collections import defaultdict
d = defaultdict(int)
d['x'] += 2
d['y']
print(dict(d))
  1. A. {'x': 2}
  2. B. {'x': 2, 'y': None}
  3. C. {'y': 0, 'x': 2}
  4. D. {'x': 2, 'y': 0}

Correct answer: D. {'x': 2, 'y': 0}

Reading a missing key from a defaultdict creates it with the default value 0, so y appears in the dict with 0.

More Python Programming questions

Exams that ask this topic