ProgrammingVery hardProduct company OA

What is printed?

What is printed?
a = list(range(8))
b = a[::2]
a[1::2] = b[::-1]
a[2:5] = []
del a[-2:]
b[0] = 99
print(a, b)
  1. A. [99, 6, 2] [99, 2, 4, 6]
  2. B. [0, 6, 2] [99, 2, 4, 6]
  3. C. [0, 6, 4] [99, 2, 4, 6]
  4. D. [0, 6, 2, 6] [99, 2, 4, 6]

Correct answer: B. [0, 6, 2] [99, 2, 4, 6]

b = a[::2] is an independent copy [0, 2, 4, 6]. Extended-slice assignment writes its reverse into the odd positions: a = [0, 6, 2, 4, 4, 2, 6, 0]. a[2:5] = [] deletes indices 2, 3, 4 → [0, 6, 2, 6, 0]; del a[-2:] leaves [0, 6, 2]. Changing b[0] afterwards cannot reach a because a slice copies. The option starting with 99 treats the slice as a view (NumPy behaviour, not list behaviour).

More Python Programming questions

Exams that ask this topic