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]))])- A. ['Meera', 'Asha', 'Ravi', 'Dev']
- B. ['Meera', 'Asha', 'Dev', 'Ravi']
- C. ['Asha', 'Meera', 'Ravi', 'Dev']
- 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
- What is printed on the second line? python items = [3, -1, 2, -3, 1] print(sorted(items, key=abs)) print(sorted(items, key=abs…
- What is printed on the second line? python data = [('b', 2), ('a', 2), ('c', 1), ('a', 1)] print(sorted(data, key=lambda t: (t[1], t[0])…
- What is printed on the second line? python words = ['bb', 'a', 'ccc', 'dd', 'e'] print(sorted(words, key=len)) print(sorted(words…
- What is printed on the last line? python nums = ['10', '9', '100', '1'] print(sorted(nums)) print(sorted(nums, key=int)) print(max(nums)…
- What is printed on the third line? python print(sorted([(1, 'x'), (1, 'X'), (0, 'z')], key=lambda t: t[1].lower())) print(sorted(['b'…
- What is printed on the second line? python u = '' for ch in 'abc': u = ch + u print(u)