ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
d = {}
d.setdefault("k", []).append(1)
d.setdefault("k", [9]).append(2)
r = d.setdefault("n", None)
print(d, r)- A. {'k': [1, 2], 'n': None} None
- B. {'k': [9, 2], 'n': None} None
- C. {'k': [1, 2]} None
- D. {'k': [1, 2], 'n': None} n
Correct answer: A. {'k': [1, 2], 'n': None} None
setdefault inserts the default only when the key is missing and always returns the stored value. The second call ignores [9] and returns the existing list, so 2 is appended to it.
More Python Programming questions
- 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})])
- What is printed? python a = [3, 1, 2] b = a.sort() or a c = sorted(a, reverse=True) a.reverse() print(b, c, b is a)
- What is printed? python data = [("b", 2), ("a", 2), ("c", 1), ("a", 1)] print(sorted(data, key=lambda t: t[1]), sorted(data)[0])
- What is printed? python def f(a, /, b, , c): return a + b + c for call in ["f(1, 2, c=3)", "f(1, b=2, c=3)", "f(a=1, b=2, c=3)", "f(1, 2…
- What is printed? python def f(a, kw): return a, kw print(f(1, {"b": 2}), f({"a": 3, "kw": 4}), end=" ") try: f(1, {"a": 2}) except…
- What is printed? python def f(x, , y=2, kw): return x y + len(kw) opts = {"y": 3, "z": 0} print(f(2, opts), f(2, z=1, w=1))