ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
print(max(['10', '9']), max(['10', '9'], key=len), sorted(['10', '9', '100'], key=lambda s: (len(s), s)))
- A. 10 10 ['9', '10', '100']
- B. 9 10 ['9', '10', '100']
- C. 9 9 ['9', '10', '100']
- D. 9 10 ['10', '9', '100']
Correct answer: B. 9 10 ['9', '10', '100']
Strings compare character by character, so '9' is greater than '10'. With key=len the longer '10' wins, and the (length, text) key sorts the one-digit string first.
More Python Programming questions
- What is printed? python print(int(True) - ~True, 4 >> True, True << 3, True + True True)
- What is printed? python print('5' 3 > '10' 2, 5 3 > 10 2, '55' < '9', 55 < 9)
- What is printed? python print(float('inf') > 21000, -float('inf') < -(21000), float('inf') == float('inf') + 1)
- What is printed? python print(repr(0.1), repr(1 / 3), repr(2 / 3), repr(1e-7), repr(100.0))
- What is printed? python print(0.1 3 != 0.3, abs(0.1 3 - 0.3) < 2-52, abs(0.1 3 - 0.3) < 2-54)
- What is printed? python print(bin(255)[2:].rjust(10, '0'), int('0b' + '1' 8, 2) == 255, int('11111111', 2) + 0 == 255)