ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
x = [0, 1, 2, 3, 4, 5]
print(x[1::2], x[-1::-2], x[::-1][::2] == x[::-2])
  1. A. [1, 3, 5] [5, 3, 1] False
  2. B. [0, 2, 4] [5, 3, 1] True
  3. C. [1, 3, 5] [5, 3, 1] True
  4. D. [1, 3, 5] [4, 2, 0] True

Correct answer: C. [1, 3, 5] [5, 3, 1] True

x[1::2] takes the odd positions. Stepping -2 from the last element gives 5, 3, 1. Reversing and then taking every second element is the same as stepping by -2 from the end.

More Python Programming questions

Exams that ask this topic