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))
- A. {'x': 2}
- B. {'x': 2, 'y': None}
- C. {'y': 0, 'x': 2}
- 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
- What is printed? python print(list(map(pow, [2, 3], [3, 2])))
- What is printed? python print(min(['10', '9', '2']), min([10, 9, 2]))
- What is printed? python s = {1, 2} t = s t |= {3} print(s, s is t)
- What is printed? python print(int(3.99), int(-3.99), round(3.5), round(4.5))
- What is printed? python print('ab' in ['ab', 'cd'], 'ab' in 'cdab', [] in [[]])
- What is printed? python x = [1, 2, 3] del x[::2] print(x)