ProgrammingHardProduct-company MCQ pattern

What is printed on the second line?

What is printed on the second line?
words = ['pear', 'fig', 'apple', 'kiwi', 'date']
print(sorted(words, key=lambda w: (-len(w), w)))
print(sorted(words, key=len, reverse=True))
  1. A. ['apple', 'date', 'kiwi', 'pear', 'fig']
  2. B. ['apple', 'pear', 'kiwi', 'date', 'fig']
  3. C. ['apple', 'pear', 'date', 'kiwi', 'fig']
  4. D. ['apple', 'kiwi', 'date', 'pear', 'fig']

Correct answer: B. ['apple', 'pear', 'kiwi', 'date', 'fig']

With reverse=True, Python keeps equal-length words in their original order, so the 4-letter words stay pear, kiwi, date. Only the 5-letter word comes first and the 3-letter word last.

More Python Programming questions

Exams that ask this topic