ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
def f(a, **kw):
    return a, kw
print(f(1, **{"b": 2}), f(**{"a": 3, "kw": 4}), end=" ")
try:
    f(1, **{"a": 2})
except TypeError:
    print("dup")
  1. A. (1, {'b': 2}) TypeError
  2. B. (1, {'b': 2}) (3, {}) dup
  3. C. (1, {'b': 2}) (3, {'kw': 4}) (1, {'a': 2})
  4. D. (1, {'b': 2}) (3, {'kw': 4}) dup

Correct answer: D. (1, {'b': 2}) (3, {'kw': 4}) dup

A key named 'kw' in ** unpacking is just another keyword argument and is collected into the kw dict. Supplying a both positionally and via **{'a': 2} gives 'multiple values for argument a', a TypeError.

More Python Programming questions

Exams that ask this topic