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__)
  1. A. 6 | 6
  2. B. 6 | TypeError
  3. C. TypeError
  4. 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

Exams that ask this topic