ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
print(sorted([('b', 1), ('a', 2), ('a', 1)])[1:], sorted({3: 'c', 1: 'a'}.items()))- A. [('a', 1), ('b', 2)] [(1, 'a'), (3, 'c')]
- B. [('a', 2), ('b', 1)] [(3, 'c'), (1, 'a')]
- C. [('b', 1), ('a', 2)] [(1, 'a'), (3, 'c')]
- D. [('a', 2), ('b', 1)] [(1, 'a'), (3, 'c')]
Correct answer: D. [('a', 2), ('b', 1)] [(1, 'a'), (3, 'c')]
Tuples sort by their first item and then their second, so the sorted list is (a,1), (a,2), (b,1), and slicing from index 1 drops (a,1). dict items sorted give pairs in key order.
More Python Programming questions
- What is printed? python print(sorted({'b': 1, 'a': 2}), sorted({'b': 1, 'a': 2}.values(), reverse=True))
- What is printed? python print(len({x % 3 for x in range(10)}), sorted({x x % 5 for x in range(5)}))
- What is printed? python print(list(range(-5, 5, 4)), list(range(5, -5, -4)), range(-1, -4, -1)[0])
- What is printed? python print(int(-7 / 2), -7 // 2, int(7 / -2), 7 // -2)
- What is printed? python print(7 % 0.5, 7.25 % 0.5, -1 % 0.25)
- What is printed? python print(1 == 1.0 == True, 2 == 2.0 != 3.0)