ProgrammingEasyTCS NQT · Infosys pattern

What is printed?

What is printed?
nums = [3, 1, 2]
r = nums.sort()
s = sorted(nums, reverse=True)
print(r, nums, s)
  1. A. None [1, 2, 3] [3, 2, 1]
  2. B. [1, 2, 3] [1, 2, 3] [3, 2, 1]
  3. C. None [3, 1, 2] [3, 2, 1]
  4. D. [1, 2, 3] [3, 1, 2] [3, 2, 1]

Correct answer: A. None [1, 2, 3] [3, 2, 1]

list.sort() sorts in place and returns None. sorted() returns a new list and leaves the original unchanged. Writing nums = nums.sort() is a common bug that loses the list.

More Python Programming questions

Exams that ask this topic