ProgrammingHardTCS NQT · Infosys

What does this Python code print? words = ["bb", "a", "ccc", "dd", "e", "fff"] print(sorted(words, key=len, reverse=True)[:3], min(words, key=len), max(words, key=len))

  1. A. ['ccc', 'fff', 'bb'] a ccc
  2. B. ['fff', 'ccc', 'dd'] e fff
  3. C. ['ccc', 'fff', 'bb'] e ccc
  4. D. ['ccc', 'fff', 'bb'] a fff

Correct answer: A. ['ccc', 'fff', 'bb'] a ccc

sorted with reverse=True is still stable, so equal-length words keep their original order: ccc, fff, bb. min and max also return the first element among ties, giving 'a' and 'ccc'.

More Python Programming questions

Exams that ask this topic