ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
def f(a, b=2, *c, d=4):
    return a + b + sum(c) + d

print(f(1, 3, 5, 7, d=1))
  1. A. TypeError
  2. B. 13
  3. C. 20
  4. D. 17

Correct answer: D. 17

a = 1 and b = 3 are filled by position, the extra 5 and 7 go into c = (5, 7), and d is given as the keyword 1. So 1 + 3 + 12 + 1 = 17. The trap is using the default d = 4, which gives 20.

More Python Programming questions

Exams that ask this topic