ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: B. [5, 4, 3] [5, 4] 5

x[-1:-4:-1] starts at the last item and goes back to, but not including, index -4, giving [5, 4, 3]. x[::2] is [1, 3, 5], whose last item is 5.

More Python Programming questions

Exams that ask this topic