ProgrammingModerateTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import heapq
print(heapq.nlargest(2, ['pear', 'fig', 'apple'], key=len))
  1. A. ['pear', 'fig']
  2. B. ['apple', 'pear']
  3. C. ['pear', 'apple']
  4. D. ['apple', 'fig']

Correct answer: B. ['apple', 'pear']

nlargest with key=len picks the two longest words: 'apple' (5 letters) and 'pear' (4). The result is in descending order of key.

More Python Programming questions

Exams that ask this topic