ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
from collections import defaultdict
d = defaultdict(list)
d["a"].append(1)
_ = d["b"]
print("c" in d, d.get("c"), len(d), dict(d))- A. False [] 3 {'a': [1], 'b': [], 'c': []}
- B. False None 1 {'a': [1]}
- C. False None 2 {'a': [1], 'b': []}
- D. True None 3 {'a': [1], 'b': [], 'c': []}
Correct answer: C. False None 2 {'a': [1], 'b': []}
Only d[key] (via __missing__) creates a default entry. Reading d['b'] creates 'b'; `in` and .get() never trigger the factory, so 'c' is never added.
More Python Programming questions
- What is printed? python from collections import Counter a = Counter("aabbbc") b = Counter("abccd") print(sorted((a - b).items())…
- What is printed? python a = {"x": 1, "y": 2} b = {"y": 3, "z": 4} c = a | b a |= {"w": 0} print(c, b | a)
- What is printed? python d = {i: i i for i in range(4)} try: for k in d: if k == 1: del d[k] except RuntimeError: print("RE", end=" ") for…
- What is printed? python d = {"a": 1} v = d.values() k = d.keys() d["b"] = 2 print(list(v), k - {"a"}, "b" in k)
- What is printed? python d = {} d.setdefault("k", []).append(1) d.setdefault("k", [9]).append(2) r = d.setdefault("n", None) print(d, r)
- What is printed? python s = {3, 1, 2} t = frozenset([1, 2]) print(t < s, s > t, s.isdisjoint({4}), {t: "ok"}[frozenset({2, 1})])