ProgrammingModerateTCS NQT · Infosys

What is printed?

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

Correct answer: C. True [3, 2, 1] [3, 2, 1]

sorted returns a new list and sort() sorts in place, but both use a descending order here, so the contents match. The printed values are the final states of a and b.

More Python Programming questions

Exams that ask this topic