ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
funcs = {}
for name in ["inc", "dbl"]:
if name == "inc":
def op(v): return v + 1
else:
def op(v): return v * 2
funcs[name] = op
print(funcs["inc"](5), funcs["dbl"](5), funcs["inc"].__name__)- A. 10 10 op
- B. 6 10 op
- C. 6 10 inc
- D. 6 6 op
Correct answer: B. 6 10 op
Each def statement creates a new function object at the moment it runs, so the dict stores two different functions; the later rebinding of op does not affect the earlier one. Both are named op.
More Python Programming questions
- What is printed? python gen = (x for x in range(3)) lst = [x for x in range(3)] print(type(gen).name, len(lst), sum(gen), sum(gen)…
- What is printed? python class A: def repr(self): return "A()" class B(A): def str(self): return "B!" print(str(A()), [B()], f"{B()}", "%s…
- What is printed? python x = 10 def f(x, y=x): x += 1 return x, y x = 20 print(f(5), f(y=1, x=2), x)
- What is printed? python for i in range(3): if i == 5: break else: print("else", i, end=" ") n = 0 while n < 3: n += 1 if n == 2: break…
- What is printed? python def find(xs, t): for i, x in enumerate(xs): if x == t: return i else: return -1 return "unreachable"…
- What is printed? python def kind(v): match v: case 0 | 1: return "bit" case int(n) if n < 0: return "neg" case [x, rest]: return…