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")- A. (1, {'b': 2}) TypeError
- B. (1, {'b': 2}) (3, {}) dup
- C. (1, {'b': 2}) (3, {'kw': 4}) (1, {'a': 2})
- 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
- What is printed? python def f(x, , y=2, kw): return x y + len(kw) opts = {"y": 3, "z": 0} print(f(2, opts), f(2, z=1, w=1))
- What is printed? python log = [] def v(x): log.append(x) return x def f(a, k): return a, k f(v(1), [v(2)], k=v(3), {"m": v(4)}) print(log)
- What is printed? python def outer(fn): print("deco", end=" ") return fn @outer def g(): print("g", end=" ") print("start", end=" ") g(); g()
- What is printed? python def a(fn): def w(): return "a(" + fn() + ")" return w def b(fn): def w(): return "b(" + fn() + ")" return w @a @b…
- What is printed? python import functools def deco(fn): @functools.wraps(fn) def wrapper(a): wrapper.n += 1 return fn(a) wrapper.n = 0…
- What is printed? python def repeat(n): print("R", n, end=" ") def deco(fn): def w(x): return [fn(x) for in range(n)] return w return deco…