ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
a = list(range(6))
try:
    a[::2] = [9, 9]
except ValueError as e:
    print("VE", end=" ")
a[1:2] = []
a[:0] = [7, 7]
print(a)
  1. A. [9, 1, 9, 3, 5]
  2. B. VE [7, 7, 0, 1, 2, 3, 4, 5]
  3. C. VE [7, 7, 0, 2, 3, 4, 5]
  4. D. VE [0, 2, 3, 4, 5, 7, 7]

Correct answer: C. VE [7, 7, 0, 2, 3, 4, 5]

An extended slice (step 2) selects 3 items, so assigning a 2-item list raises ValueError and changes nothing. Then a[1:2] = [] deletes 1, and a[:0] = [...] inserts at the front.

More Python Programming questions

Exams that ask this topic