ProgrammingModerateTCS CodeVita · Infosys HackWithInfy

What is printed?

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

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

sort() works in place and returns None, but sorted() always returns a new list. nums and the new sorted list are different objects, so the identity test is False.

More Python Programming questions

Exams that ask this topic