ProgrammingModerateTCS NQT · Python

What is printed?

What is printed?
a = [1, 2, 3]
b = a
b += [4]
c = a
c = c + [5]
print(a, c)
  1. A. [1, 2, 3] [1, 2, 3, 5]
  2. B. [1, 2, 3, 4] [1, 2, 3, 5]
  3. C. [1, 2, 3] [1, 2, 3, 4, 5]
  4. D. [1, 2, 3, 4] [1, 2, 3, 4, 5]

Correct answer: D. [1, 2, 3, 4] [1, 2, 3, 4, 5]

`+=` extends the shared list in place, so `a` sees 4; `c + [5]` builds a new list bound to `c`, leaving `a` unchanged.

More Python Programming questions

Exams that ask this topic