ProgrammingHardProduct-company MCQ pattern

What is printed on the third line?

What is printed on the third line?
emp = [('Ravi', 30, 'IT'), ('Asha', 25, 'HR'), ('Meera', 30, 'HR'), ('Dev', 25, 'IT')]
by_age = sorted(emp, key=lambda e: e[1])
print([e[0] for e in by_age])
print([e[0] for e in sorted(by_age, key=lambda e: e[2])])
print([e[0] for e in sorted(emp, key=lambda e: (e[2], -e[1]))])
  1. A. ['Meera', 'Asha', 'Ravi', 'Dev']
  2. B. ['Meera', 'Asha', 'Dev', 'Ravi']
  3. C. ['Asha', 'Meera', 'Ravi', 'Dev']
  4. D. ['Ravi', 'Meera', 'Asha', 'Dev']

Correct answer: A. ['Meera', 'Asha', 'Ravi', 'Dev']

Sorting on (department, -age) puts HR first, then IT, and within each department the older employee comes first. Ties in one key are resolved by the other key, not by input order.

More Python Programming questions

Exams that ask this topic