Functions & Scope · Question 17 · Hard
A default parameter value refers to a module-level variable that is changed later. What does interest(1000) print?
rate = 5
def interest(p, r=rate):
return p * r // 100
rate = 10
print(interest(1000))
- 100
- 50
- 0
- NameError
Answer and explanation
Answer: B. 50
Explanation: Default values are evaluated once, at the time the def statement runs. At that moment rate was 5, so r stays 5 even after rate becomes 10, and 1000 × 5 // 100 = 50.
Previous: The enclosing variable x is reassigned after the inner function is defined but… · Next: The recursive function below counts how many moves a number needs to reach 1…
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
- When Python looks up a name used inside a function, in which order does it…
- A function signature mixes normal parameters, *rest and a keyword-only…
- The enclosing variable x is reassigned after the inner function is defined but…
- The recursive function below counts how many moves a number needs to reach 1…
- Which statement about the nonlocal keyword in Python is correct?
- Inside update, one list is changed with append and the other name is…
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