ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
words = ['bb', 'a', 'ccc']
print(max(words), max(words, key=len), min(words, key=len), sorted(words, key=lambda w: (len(w), w))[-1])
  1. A. bb ccc a ccc
  2. B. ccc ccc a bb
  3. C. ccc bb a ccc
  4. D. ccc ccc a ccc

Correct answer: D. ccc ccc a ccc

Without a key, max compares strings alphabetically, so 'ccc' wins. The key len picks the longest and shortest words, and the sorted list ends with the longest word.

More Python Programming questions

Exams that ask this topic