ProgrammingHardTCS NQT · Accenture

What is printed?

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

Correct answer: D. [] [2, 3] [1]

Slices never raise IndexError. a[3:] starts past the end and is empty, a[1:10] clips to the end, and a[-10:1] clips the start to 0.

More Python Programming questions

Exams that ask this topic