ProgrammingVery hardTCS NQT · Wipro pattern
What is printed?
What is printed?
from functools import cache
@cache
def total(xs):
return sum(xs)
try:
print(total((1, 2, 3)))
print(total([1, 2, 3]))
except TypeError as e:
print(type(e).__name__)- A. 6 | 6
- B. 6 | TypeError
- C. TypeError
- D. 6 | 1
Correct answer: B. 6 | TypeError
The cache keys its dictionary by the arguments, so a list argument raises TypeError because lists are unhashable. The tuple call prints 6 first, and the except branch prints the exception name.
More Python Programming questions
- You must choose the maximum number of non-overlapping activities, each with a start and a finish time. Which greedy rule is correct?
- What is printed? python a = [4, 1, 7, 3, 2] pre = [0] for x in a: pre.append(pre[-1] + x) def rng(l, r): return pre[r] - pre[l - 1]…
- What is printed? python def mostwater(h): i, j, best = 0, len(h) - 1, 0 while i < j: best = max(best, min(h[i], h[j]) (j - i)) if h[i] <…
- What is printed? python def longest(s): last = {} l = best = 0 for r, ch in enumerate(s): if ch in last and last[ch] >= l: l = last[ch] +…
- What is printed? python import bisect a = sorted([5, 1, 9, 5, 3, 7, 5]) lo = bisect.bisectleft(a, 3) hi = bisect.bisectright(a, 7)…
- A Python program must answer 10^6 membership queries (x in container) against a container with 10^6 items. Which statement is correct?