ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
a = {"x": 1, "y": 2}
b = {"y": 3, "z": 4}
c = a | b
a |= {"w": 0}
print(c, b | a)- A. {'x': 1, 'y': 2, 'z': 4} {'y': 3, 'z': 4, 'x': 1, 'w': 0}
- B. {'x': 1, 'y': 3, 'z': 4} {'y': 3, 'z': 4, 'x': 1, 'w': 0}
- C. {'x': 1, 'y': 3, 'z': 4} {'x': 1, 'y': 2, 'z': 4, 'w': 0}
- D. {'x': 1, 'y': 3, 'z': 4} {'y': 2, 'z': 4, 'x': 1, 'w': 0}
Correct answer: D. {'x': 1, 'y': 3, 'z': 4} {'y': 2, 'z': 4, 'x': 1, 'w': 0}
In d1 | d2 the right operand's values win but key order starts with the left operand's keys. So b | a keeps b's order (y, z) then adds x, w, with y taking a's value 2.
More Python Programming questions
- 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})])
- 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])