ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
a = [1, 2, 3, 4, 5]
a[1:4] = "xy"
b = a[:]
b[::-1] = b
print(a, b[0])
  1. A. [1, 'xy', 5] 5
  2. B. [1, 'x', 'y', 5] 1
  3. C. [1, 'x', 'y', 4, 5] 5
  4. D. [1, 'x', 'y', 5] 5

Correct answer: D. [1, 'x', 'y', 5] 5

Slice assignment accepts any iterable, so the string contributes two items 'x' and 'y' replacing 2, 3, 4. Assigning b to its own reversed extended slice reverses it (the right side is copied first), so b[0] becomes 5.

More Python Programming questions

Exams that ask this topic