ProgrammingHardGATE CS · Product-company placement

What is printed?

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

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

a = a * 2 builds a NEW list and rebinds a, leaving b's three references pointing at the old [1, 2]; appending 9 through b[0] is therefore seen in b[1] but not in the new a.

More Python Programming questions

Exams that ask this topic