ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
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))- A. 6 6
- B. 7 4
- C. 8 6
- D. 7 6
Correct answer: D. 7 6
**opts supplies y = 3 and puts z into kw: 2*3 + 1 = 7. In the second call y keeps its default 2 and kw has two items: 2*2 + 2 = 6.
More Python Programming questions
- 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…
- What is printed? python calls = [] def trace(fn): calls.append(fn.name) return fn class C: @trace def m(self): pass @staticmethod @trace…