ProgrammingHardIBM · Deloitte · Product Company SDE
What does the following Python 3 code print? def f(a, *, b): return a + b try: print(f(1, 2)) except TypeError: print("err")
- A. err
- B. 1 2
- C. TypeError: f() takes 1 argument
- D. 3
Correct answer: A. err
The bare * forces b to be supplied only as a keyword; calling f(1, 2) passes 2 positionally and raises a TypeError, caught here.
More Python Programming questions
- What does the following Python 3 code print? l = [1, 2, 3] print(l, sep="-")
- What does the following Python 3 code print? print([1, 2, 3], [4, 5], sep=",")
- What does the following Python 3 code print? d = {"a": 1, "b": 2} print(d)
- What does the following Python 3 code print? a, b, c = [1, 2, 3, 4, 5] print(a, b, c)
- What does the following Python 3 code print? def power(base, exp=2): return base exp print(power(3), power(3, 3), power(exp=3, base=2))
- What does the following Python 3 code print? class Stack: def init(self): self.items = [] def len(self): return len(self.items) def…