ProgrammingModerateTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: C. [1, 9, 9, 9, 9, 4, 5]

Assigning to a plain slice replaces the slice with any iterable, and the length can grow. Here the two items at indices 1 and 2 are replaced by four 9s.

More Python Programming questions

Exams that ask this topic