ProgrammingHardProduct-company MCQ pattern
What is printed on the second line?
What is printed on the second line?
words = ['pear', 'fig', 'apple', 'kiwi', 'date'] print(sorted(words, key=lambda w: (-len(w), w))) print(sorted(words, key=len, reverse=True))
- A. ['apple', 'date', 'kiwi', 'pear', 'fig']
- B. ['apple', 'pear', 'kiwi', 'date', 'fig']
- C. ['apple', 'pear', 'date', 'kiwi', 'fig']
- D. ['apple', 'kiwi', 'date', 'pear', 'fig']
Correct answer: B. ['apple', 'pear', 'kiwi', 'date', 'fig']
With reverse=True, Python keeps equal-length words in their original order, so the 4-letter words stay pear, kiwi, date. Only the 5-letter word comes first and the 3-letter word last.
More Python Programming questions
- 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))
- What is printed? python print(7 // -2, -7 // 2, 7.0 // -2, -7.0 % 2)