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))- A. TypeError 3
- B. 3 3
- C. TypeError TypeError
- 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
- What is printed? python def n(kw): return kw print(n({"a": 1}, {"b": 2}))
- What is printed? python def p(a, b, c=3): return a, b, c d = {"a": 1, "b": 2, "c": 10} print(p(d))
- What is printed? python def r(args): return sum(args) l = [1, 2, 3] print(r(l, l))
- What is printed? python import copy a = [[1, 2], [3, 4]] b = copy.deepcopy(a) b[0][0] = 99 print(a)
- What is printed? python import copy t = ([1, 2], [3, 4]) t2 = copy.deepcopy(t) t2[0].append(9) print(t)
- What is printed? python import copy a = [1, 2, 3] b = a.copy() c = list(a) d = a[:] e = copy.copy(a) a.append(4) print(b, c, d, e)