Built-ins & Standard Library · Question 19 · Hard
A defaultdict(int) is updated for one key, read for a second key and tested with in for a third. What is printed?
from collections import defaultdict
d = defaultdict(int)
d["a"] += 1
x = d["b"]
print(len(d), "c" in d, len(d))
- 1 False 1
- 2 False 2
- 2 False 3
- 2 True 3
Answer and explanation
Answer: B. 2 False 2
Explanation: Reading d["b"] with square brackets inserts the key b with the default 0, so the dictionary has 2 keys. The in test only checks for the key and never creates it, so the length stays 2.
Previous: A program imports the same module helper from two different files. How many… · Next: all, any and max are each given an empty list, with a default supplied to max…
Practise built-ins & standard library
Try a fresh set of built-ins & standard library questions with an explanation after every answer, free and without signing up: Built-ins & Standard Library practice. Or sit a full placement mock test.
More built-ins & standard library questions
- itertools.groupby is applied directly to a string in which one letter appears…
- Booleans are used in an isinstance check, in addition and inside sum. What…
- A program imports the same module helper from two different files. How many…
- all, any and max are each given an empty list, with a default supplied to max…
More python practice
Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge