ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
def k(a, *, b):
    return a + b
try:
    k(1, 2)
except TypeError:
    print("TypeError")
print(k(1, b=2))
  1. A. TypeError 3
  2. B. 3 3
  3. C. TypeError TypeError
  4. D. 3 TypeError

Correct answer: A. TypeError 3

The bare * marks b as keyword-only, so it cannot be passed positionally: k(1, 2) raises TypeError. Passing it as k(1, b=2) works and returns 1 + 2 = 3.

More Python Programming questions

Exams that ask this topic