ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
pairs = [(2, 'b'), (1, 'z'), (2, 'a'), (1, 'y')] print(sorted(pairs, key=lambda t: t[0])) print(sorted(pairs, key=lambda t: t[0], reverse=True))
- A. [(2, 'b'), (2, 'a'), (1, 'z'), (1, 'y')]
- B. [(2, 'a'), (2, 'b'), (1, 'y'), (1, 'z')]
- C. [(1, 'z'), (1, 'y'), (2, 'b'), (2, 'a')]
- D. [(2, 'b'), (2, 'a'), (1, 'y'), (1, 'z')]
Correct answer: A. [(2, 'b'), (2, 'a'), (1, 'z'), (1, 'y')]
Both sorts are stable, and reverse=True keeps the original order of equal keys rather than reversing them. The second line therefore lists the key-2 items in input order (b, a) and then the key-1 items in input order (z, y).
More Python Programming questions
- What is printed on the second line? python words = ['pear', 'fig', 'apple', 'kiwi', 'date'] print(sorted(words, key=lambda w: (-len(w)…
- What is printed? python print(('ab' 3).count('aba'), 'ababa'.count('aba'), 'aaaa'.count('aa'))
- What is printed on the third line? python print('a,b,,c'.split(',')) print(' a b '.split()) print(' a b '.split(' '))
- What is printed? python print(int('0x1f', 16), int('0x1f', 0), int(' -12 '), int('1000'))
- What is printed? python print(bin(-5), format(-5, '08b'), (-5) & 0xFF, format(-5 & 0xFF, '08b'))
- What is printed? python print(divmod(-17, 5), divmod(17, -5), divmod(-17.0, 5))