ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
print(max(range(5), key=lambda i: (i % 3, -i)))
  1. A. 1
  2. B. 4
  3. C. 0
  4. D. 2

Correct answer: D. 2

The key (i % 3, -i) is largest when i % 3 is 2, which happens only at i = 2. The tuple's second item breaks ties in favour of smaller i.

More Python Programming questions

Exams that ask this topic