ProgrammingHardTCS NQT · Infosys pattern

What is printed?

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

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

deepcopy recursively copies every nested object, so b's inner lists are entirely independent from a's. Changing b[0][0] has no effect on a.

More Python Programming questions

Exams that ask this topic