ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
x = [0] * 3
x[0] += 1
x = x * 2
x[3] += 5
print(x)
  1. A. [1, 0, 5, 0, 0, 0]
  2. B. [1, 0, 0, 6, 0, 0]
  3. C. [1, 0, 0, 1, 0, 0]
  4. D. [1, 0, 0, 5, 0, 0]

Correct answer: B. [1, 0, 0, 6, 0, 0]

x becomes [1, 0, 0] and then [1, 0, 0, 1, 0, 0] after the multiplication. Index 3 is the first item of the second copy, which is 1, so adding 5 gives 6.

More Python Programming questions

Exams that ask this topic