Advanced Collections — Series 2

Preview — 3 of 10 questions

What is the output of the following code?

javascript
d = {"a": 1, "b": 2}
print(d.pop("a"))
print(d)
print(d.pop("z", "missing"))
d.pop("z")
A1, {'b': 2}, missing, KeyError: 'z'
B1, {'b': 2}, None, KeyError: 'z' — the default argument to .pop() is ignored; it always returns None for a missing key.
C1, {'a': 1, 'b': 2}, missing, KeyError: 'z' — .pop() doesn't actually remove the key, just returns its value.
D1, {'b': 2}, missing, None — a genuinely missing key with no default silently returns None instead of raising.

What is the output of the following code?

javascript
d = {"a": 1, "b": 2, "c": 3}
print(d.popitem())
print(d)
print(d.popitem())
print(d)
A('a', 1), {'b': 2, 'c': 3}, ('b', 2), {'c': 3} — .popitem() removes the *first*-inserted item (FIFO).
B('c', 3), {'a': 1, 'b': 2}, ('b', 2), {'a': 1}
C('c', 3), {'a': 1, 'b': 2}, ('a', 1), {'b': 2} — each call removes a random remaining item, not necessarily the most recent.
DTypeError: popitem() takes no arguments on the first call

What is the output of the following code?

javascript
nums = [1, 2, 3]
result = nums.reverse()
print(result)
print(nums)

nums2 = [1, 2, 3]
result2 = reversed(nums2)
print(result2)
print(nums2)
print(list(result2))
A[3, 2, 1], [3, 2, 1], then [3, 2, 1], [3, 2, 1], [3, 2, 1] — both methods mutate in place and return the reversed list.
BNone, [3, 2, 1], then [3, 2, 1], [3, 2, 1], [3, 2, 1] — reversed() also mutates its argument in place.
CNone, [3, 2, 1], then <list_reverseiterator object at ...>, [1, 2, 3], [3, 2, 1]
DNone, [3, 2, 1], then [3, 2, 1], [3, 2, 1], [3, 2, 1] — reversed() returns an already-materialized list, not a lazy iterator.

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.