ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
print(max(-3, 2, key=abs), min(-3, 2, key=abs), sorted([-3, 2, -2], key=abs), sorted([-3, 2, -2], key=lambda v: -abs(v)))
  1. A. 2 -3 [2, -2, -3] [-3, 2, -2]
  2. B. -3 2 [2, -2, -3] [-3, 2, -2]
  3. C. -3 2 [-2, 2, -3] [-3, 2, -1]
  4. D. -3 2 [2, -2, -3] [-3, -2, 5]

Correct answer: B. -3 2 [2, -2, -3] [-3, 2, -2]

max and min with key=abs compare absolute values, so -3 wins the max and 2 wins the min. Sorting by abs keeps the input order for ties between 2 and -2, and sorting by -abs puts the largest magnitude first.

More Python Programming questions

Exams that ask this topic