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)))
  1. A. {'b': 2, 'c': 3} {'b': 2, 'c': 3} {'x': 2} {'a': 'c', 'b': 'd'}
  2. B. {'a': 1, 'b': 2} {'c': 3} {'x': 1} {'a': 'c', 'b': 'd'}
  3. C. {'a': 1, 'b': 2} {'a': 1, 'c': 3} {'x': 2} {'a': 'c', 'b': 'd'}
  4. 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

Exams that ask this topic