ProgrammingVery hardPython · GATE DA · Advanced

What is printed?

What is printed?
a = list(range(6))
a[1:4] = 'xy'
print(a)
try:
    a[::2] = [0, 0]
except ValueError as e:
    print('VE')
a[2:2] = [7]
print(a, len(a))
  1. A. [0, 'x', 'y', 4, 5] / [0, 0, 7, 0, 4, 5] 5
  2. B. [0, 'xy', 4, 5] / VE / [0, 'xy', 7, 4, 5] 5
  3. C. [0, 'x', 'y', 4, 5] / VE / [0, 'x', 7, 'y', 4, 5] 6
  4. D. [0, 'x', 'y', 4, 5] / VE / [0, 7, 'x', 'y', 4, 5] 6

Correct answer: C. [0, 'x', 'y', 4, 5] / VE / [0, 'x', 7, 'y', 4, 5] 6

A plain slice assignment iterates the right side (so 'xy' becomes two items) and may change the length; an extended slice (step 2) needs exactly matching length, here 3 slots vs 2 values, so ValueError. a[2:2] inserts.

More Python Programming questions

Exams that ask this topic