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))
  1. 100
  2. 50
  3. 0
  4. 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

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