ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
d = {"k": []}
e = dict(d)
f = {k: v[:] for k, v in d.items()}
d["k"].append(1)
e["k"] += [2]
f["k"] += [3]
print(d["k"], f["k"])
  1. A. [1] [3]
  2. B. [1, 2] [3]
  3. C. [1, 2, 3] [3]
  4. D. [1, 2] [1, 3]

Correct answer: B. [1, 2] [3]

dict(d) copies only the references, so e['k'] is d's list and += extends it in place, giving [1, 2]. f holds a separate slice copy made before the appends, so only 3 lands there.

More Python Programming questions

Exams that ask this topic