Internals & Memory — Series 2

Preview — 3 of 10 questions

What is the output of the following code?

javascript
d = dict.fromkeys(["a", "b", "c"], [])
d["a"].append(1)
print(d)
print(d["a"] is d["b"])
A{'a': [1], 'b': [1], 'c': [1]}, True
B{'a': [1], 'b': [], 'c': []}, False
C{'a': [1]} — only the explicitly mutated key appears in the resulting dict.
DTypeError: fromkeys() default value must be immutable

What is the output of the following code?

javascript
fs = frozenset([1, 2, 3])
s = {3, 4, 5}

result1 = fs | s
result2 = s | fs

print(type(result1))
print(type(result2))
A<class 'set'>, <class 'set'> — mixing a frozenset with a set always downgrades the result to a mutable set.
B<class 'frozenset'>, <class 'set'>
C<class 'frozenset'>, <class 'frozenset'> — a frozenset operand always makes the result immutable, regardless of position.
DTypeError: unsupported operand type(s) for |: 'frozenset' and 'set'

What is the output of the following code?

javascript
from collections import ChainMap

base = {"a": 1}
cm = ChainMap(base)
scoped = cm.new_child({"b": 2})

print(scoped["a"], scoped["b"])
print(len(cm.maps))

scoped["a"] = 100
print(base)
print(cm["a"])
A1 2, 2, {'a': 100}, 100 — new_child() shares the same underlying maps as cm, so writes propagate back to base.
B1 2, 1, {'a': 100}, 100
C1 2, 1, {'a': 1}, 1
DTypeError: new_child() missing 1 required positional argument

Sign up free to play

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