ProgrammingModerateTCS NQT · Infosys pattern

What is printed?

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

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

A shallow copy makes a new outer list but shares the inner lists, so appending 9 to a[0] shows in b, while appending [4] to a does not. deepcopy copies the inner lists too, so c is unaffected.

More Python Programming questions

Exams that ask this topic