Functions & Scope · Question 15 · Medium

A function signature mixes normal parameters, *rest and a keyword-only parameter. What is printed for this call?

def f(a, b, *rest, c=10):
    return a + b + len(rest) + c

print(f(1, 2, 3, 4))
  1. 20
  2. 8
  3. 15
  4. TypeError
Answer and explanation

Answer: C. 15

Explanation: a = 1 and b = 2; the extra positional values go into rest = (3, 4), whose length is 2. c can only be set by keyword, so it stays 10. The total is 1 + 2 + 2 + 10 = 15.

Previous: When Python looks up a name used inside a function, in which order does it… · Next: The enclosing variable x is reassigned after the inner function is defined but…

Practise functions & scope

Try a fresh set of functions & scope questions with an explanation after every answer, free and without signing up: Functions & Scope practice. Or sit a full placement mock test.

More functions & scope questions

More python practice

Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge