ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
print(sorted({'b': 1, 'a': 2}), sorted({'b': 1, 'a': 2}.values(), reverse=True))- A. ['a', 'b'] [2, 1]
- B. ['b', 'a'] [2, 1]
- C. ['a', 'b'] [1, 2]
- D. ['a', 'b'] [1, 2, 3]
Correct answer: A. ['a', 'b'] [2, 1]
Iterating a dict gives its keys, so sorted returns the keys alphabetically, and .values() gives the values, which are then sorted in reverse.
More Python Programming questions
- 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)
- What is printed? python print(3 -2 9, (3 -2) 9 == 1.0, 3 -2 9 == 1)