ProgrammingHardCAT · MBA placement

What is printed?

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

Correct answer: A. [[1, 9], [2]] [[1], [2]]

a[:] copies only the outer list, so b shares the inner list that receives 9. deepcopy copies the nested lists, so c is unaffected.

More Python Programming questions

Exams that ask this topic