ProgrammingHardHCL · Tech Mahindra · Python placement
What does the following Python 3 code print? def f(**kwargs): return sorted(kwargs.items()) print(f(b=2, a=1, c=3))
- A. [('a', 1), ('b', 2), ('c', 3)]
- B. [(1, 'a'), (2, 'b'), (3, 'c')]
- C. [('c', 3), ('b', 2), ('a', 1)]
- D. [('b', 2), ('a', 1), ('c', 3)]
Correct answer: A. [('a', 1), ('b', 2), ('c', 3)]
kwargs collects the keyword arguments as a dict regardless of call order; sorted() on items() orders them alphabetically by key.
More Python Programming questions
- What does the following Python 3 code print? def f(a, , b): return a + b try: print(f(1, 2)) except TypeError: print("err")
- What does the following Python 3 code print? l = [1, 2, 3] print(l, sep="-")
- What does the following Python 3 code print? print([1, 2, 3], [4, 5], sep=",")
- What does the following Python 3 code print? d = {"a": 1, "b": 2} print(d)
- What does the following Python 3 code print? a, b, c = [1, 2, 3, 4, 5] print(a, b, c)
- What does the following Python 3 code print? def power(base, exp=2): return base exp print(power(3), power(3, 3), power(exp=3, base=2))