ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: D. there hi ['hi', 'hey', 'there']

max compares strings lexicographically, so 'there' is largest. min by length picks the first shortest word, 'hi'. The sort key is (length, word), which puts 'hi' before 'hey' and then 'there'.

More Python Programming questions

Exams that ask this topic