ProgrammingHardProduct-company MCQ 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(len(b), b[0], c[0])
  1. A. 2 [1, 2] [1, 2]
  2. B. 3 [1, 2, 9] [1, 2, 9]
  3. C. 2 [1, 2, 9] [1, 2]
  4. D. 3 [1, 2, 9] [1, 2]

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

copy.copy makes a new outer list that shares the inner lists. a.append([4]) does not change b's length (still 2), but a[0].append(9) changes the shared inner list, so b[0] shows 9. deepcopy copied the inner lists too, so c[0] is still [1, 2].

More Python Programming questions

Exams that ask this topic