ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
nums = [10, 20, 30, 40, 50]
print(nums[-2::-2], nums[1:-1][::-1], nums[::-1][1:-1])
  1. A. [40, 20] [40, 30, 20] [20, 30, 40]
  2. B. [40, 20] [40, 30, 20] [40, 30, 20]
  3. C. [40, 20, 0] [40, 30, 20] [40, 30, 20]
  4. D. [50, 30, 10] [40, 30, 20] [40, 30, 20]

Correct answer: B. [40, 20] [40, 30, 20] [40, 30, 20]

nums[-2::-2] starts at 40 and steps back two: 40, 20. Trimming the ends and then reversing gives the same middle [40, 30, 20] as reversing first and then trimming.

More Python Programming questions

Exams that ask this topic