ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
a = [1, 2, 3] print(a[3:], a[1:10], a[-10:1])
- A. [3] [2, 3] [1]
- B. [] [2, 3] []
- C. [] [1, 2, 3] [1]
- 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
- What is printed? python print(len(bytes('é', 'utf-8')), len('é'))
- What is printed? python print(float('inf') > 10308, -float('inf') < -10308)
- What is printed? python print(sorted({3: 'a', 1: 'b'}), list(reversed(sorted({3: 'a', 1: 'b'}))))
- What is printed? python print('a' < 'B', 'a'.upper() < 'B')
- What is printed? python print(0 == False, 0 is False, '' == None)
- What is printed? python print(any(x > 2 for x in [1, 2, 3]), next(x for x in [1, 2, 3] if x > 1))