ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
print(sorted([3, 1, 2], reverse=True) == [3, 2, 1], [3, 1, 2][::-1], sorted('cab')[::-1])
  1. A. False [2, 1, 3] ['c', 'b', 'a']
  2. B. True [3, 2, 1] ['c', 'b', 'a']
  3. C. True [2, 1, 3] ['c', 'b', 'a']
  4. D. True [2, 1, 3] ['a', 'b', 'c']

Correct answer: C. True [2, 1, 3] ['c', 'b', 'a']

Sorting in descending order gives [3, 2, 1], so the comparison holds. Slicing with [::-1] reverses the list as it is, not sorted, and sorted('cab') is ['a', 'b', 'c'] before reversing.

More Python Programming questions

Exams that ask this topic