ProgrammingHardGATE CS · Product-company placement
What is printed? (Output lines are shown separated by " / ".)
What is printed? (Output lines are shown separated by " / ".)
from functools import partial
def power(base, exp):
return base ** exp
sq = partial(power, exp=2)
print(sq(5), sq(5, exp=3))
try:
print(sq(2, 3))
except TypeError:
print("TypeError")- A. 25 8 / 8
- B. 25 125 / 8
- C. 25 125 / TypeError
- D. 25 25 / TypeError
Correct answer: C. 25 125 / TypeError
Keywords frozen by partial can be overridden by keywords at call time (exp=3 gives 125). sq(2, 3) passes 3 positionally to exp while partial also supplies exp=2, so Python reports multiple values for exp: TypeError.
More Python Programming questions
- What is printed? python x = [3, 1, 2] y = sorted(x) z = reversed(x) x.append(0) print(y, list(z))
- What is printed? python m = map(lambda v: v 2, [1, 2, 3]) f = filter(None, [0, 1, 2]) print(list(m), list(m), next(f), list(zip(f, "xyz")))
- What is printed? python a = [1, 2, 3] for i, v in enumerate(a): a[i] = v 2 if i == 0: a.append(9) if len(a) > 4: break print(a)
- What is printed? python x = 0 def f(): global x x += 1 return x d = {f(): f(), f(): f()} L = [f(), f()] L[f() - 7] = f() print(d, L)
- What is printed? python def f(): return f.n f.n = 5 g = f f = lambda: 0 try: print(g()) except AttributeError: print("AE", g.n)
- What is printed? python words = ["kiwi", "fig", "apple", "date"] print(sorted(words, key=len), max(words, key=len), min(words))