ProgrammingVery hardPython · GATE DA · Advanced

What is printed?

What is printed?
import copy
a = [1, 2]
b = [a, a]
c = copy.deepcopy(b)
d = copy.copy(b)
c[0].append(3)
print(b, c[0] is c[1], d[0] is a)
  1. A. [[1, 2, 3], [1, 2, 3]] True True
  2. B. [[1, 2], [1, 2]] True True
  3. C. [[1, 2, 3], [1, 2, 3]] False True
  4. D. [[1, 2], [1, 2]] False True

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

deepcopy keeps one copy per original object through its memo, so c[0] is c[1] stays True while the original b is untouched; copy.copy shares the inner list a.

More Python Programming questions

Exams that ask this topic