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))

  1. A. [('a', 1), ('b', 2), ('c', 3)]
  2. B. [(1, 'a'), (2, 'b'), (3, 'c')]
  3. C. [('c', 3), ('b', 2), ('a', 1)]
  4. 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

Exams that ask this topic