ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
d = dict(a=1)
d2 = d
d = {**d, "b": 2}
d2["c"] = 3
del d2["a"]
print(d, d2, dict([("x", 1), ("x", 2)]), dict(zip("ab", "cd", strict=False)))- A. {'b': 2, 'c': 3} {'b': 2, 'c': 3} {'x': 2} {'a': 'c', 'b': 'd'}
- B. {'a': 1, 'b': 2} {'c': 3} {'x': 1} {'a': 'c', 'b': 'd'}
- C. {'a': 1, 'b': 2} {'a': 1, 'c': 3} {'x': 2} {'a': 'c', 'b': 'd'}
- D. {'a': 1, 'b': 2} {'c': 3} {'x': 2} {'a': 'c', 'b': 'd'}
Correct answer: D. {'a': 1, 'b': 2} {'c': 3} {'x': 2} {'a': 'c', 'b': 'd'}
{**d, ...} builds a new dict, so later changes through d2 (the old object) do not affect it. When a key repeats while building a dict, the last value wins.
More Python Programming questions
- What is printed? python def f(a, b=[]): b = b or [] b.append(a) return b x = f(1) y = f(2, []) z = f(3, [0]) print(x, y, z, f.defaults)
- What is printed? python def f(x=[]): x += [len(x)] return x def g(x=()): x += (len(x),) return x print(f(), f(), g(), g())
- What is printed? python funcs = {} for name in ["inc", "dbl"]: if name == "inc": def op(v): return v + 1 else: def op(v): return v 2…
- What is printed? python gen = (x for x in range(3)) lst = [x for x in range(3)] print(type(gen).name, len(lst), sum(gen), sum(gen)…
- What is printed? python class A: def repr(self): return "A()" class B(A): def str(self): return "B!" print(str(A()), [B()], f"{B()}", "%s…
- What is printed? python x = 10 def f(x, y=x): x += 1 return x, y x = 20 print(f(5), f(y=1, x=2), x)