ProgrammingHardProduct-company MCQ pattern

What is printed?

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

Correct answer: D. [0, 9, 0, 5]

a[1:3] = [9] replaces two items with one, giving [1, 9, 4, 5]. The extended slice a[::2] then covers indices 0 and 2 (two places), and the two zeros fit exactly, giving [0, 9, 0, 5]. With a wrong count, an extended slice assignment would raise ValueError.

More Python Programming questions

Exams that ask this topic