ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
a = [1, [2, 3]]
b = a[:]
c = list(a)
d = [*a]
a[1] += [4]
a[0] += 10
print(b, c is d, d[1] is a[1])
  1. A. [11, [2, 3, 4]] False True
  2. B. [1, [2, 3, 4]] False True
  3. C. [1, [2, 3]] False False
  4. D. [1, [2, 3, 4]] True True

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

Slicing, list() and [*a] all make shallow copies: new outer lists sharing the inner list. a[1] += [4] extends that shared inner list in place; a[0] += 10 just rebinds a slot in a.

More Python Programming questions

Exams that ask this topic