ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
d = {"b": 2, "a": 1}
print(sorted(d.items()), sorted(d, key=d.get), list(reversed(sorted(d.values()))), {**d, "a": 0} == {"a": 0, "b": 2})- A. [('a', 1), ('b', 2)] ['a', 'b'] [2, 1] True
- B. [('b', 2), ('a', 1)] ['a', 'b'] [2, 1] True
- C. [('a', 1), ('b', 2)] ['b', 'a'] [2, 1] True
- D. [('a', 1), ('b', 2)] ['a', 'b'] [1, 2] False
Correct answer: A. [('a', 1), ('b', 2)] ['a', 'b'] [2, 1] True
Sorting items compares the key first. Sorting keys by d.get orders them by value. Dict equality ignores order, and in {**d, 'a': 0} the later 'a' wins.
More Python Programming questions
- What is printed? python from collections import Counter c = Counter(a=3, b=1) c.update("aab") c.subtract({"b": 5}) print(c["a"], c["b"]…
- What is printed? python from collections import ChainMap defaults = {"color": "red", "size": 1} user = {"size": 3} cm = ChainMap(user…
- What is printed? python import bisect grades = "FDCBA" cut = [60, 70, 80, 90] print([grades[bisect.bisect(cut, s)] for s in [59, 60, 85…
- What is printed? python x = [1, 2, 3] y = [x, x] y[0] = y[0] + [4] y[1] += [5] print(x, y)
- What is printed? python def f(a, b=[]): b = b + [a] f.defaults = (b,) return b print(f(1), f(2), f(3, []), f(4))
- What is printed? python it = (x x for x in range(5)) first = next(it) rest = [v for v in it if v % 2 == 0] again = sum(1 for in it)…