ProgrammingHardProduct-company MCQ pattern

What is printed?

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

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

a.copy(), list(a), a[:] and copy.copy(a) all produce four SEPARATE shallow copies of the flat list made before the append; appending to a afterward changes only a, leaving all four copies at [1, 2, 3].

More Python Programming questions

Exams that ask this topic