ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

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

b += [4] changes the list that a and b both name. c is built from that list before a is rebound, and a * 2 creates a new list for a.

More Python Programming questions

Exams that ask this topic