ProgrammingEasyInfosys · Wipro

What does this Python code print? a = [1, 2, 3] b = a a += [4] a = a + [5] print(b, a)

  1. A. [1, 2, 3, 4] [1, 2, 3, 4, 5]
  2. B. [1, 2, 3] [1, 2, 3, 4, 5]
  3. C. [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
  4. D. [1, 2, 3, 4] [1, 2, 3, 5]

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

a += [4] extends the shared list in place, so b sees it. a = a + [5] builds a new list and rebinds a, leaving b unchanged.

More Python Programming questions

Exams that ask this topic