Functions & Scope · Question 6 · Easy
A function assigns to a name that also exists at module level. What does this code print for x?
x = 10
def change():
x = 20
change()
print(x)
- 20
- None
- UnboundLocalError
- 10
Answer and explanation
Answer: D. 10
Explanation: The assignment inside change creates a new local variable x that disappears when the function ends. The module-level x is untouched, so 10 is printed.
Previous: The function below collects any number of marks using *marks. What is printed? · Next: A function is defined as def book(src, dst, seats=1). Which of these calls is…
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
- In Python, what value does a function give back when its body ends without…
- A lambda is given a default value for its second parameter. What does this…
- The function below collects any number of marks using *marks. What is printed?
- A function is defined as def book(src, dst, seats=1). Which of these calls is…
- The tag function uses a dictionary as a default parameter value. What do the…
- A function receives keyword arguments through **info. What does sorted(info)…
- The hit function tries to increase a module-level counter without declaring it…
- This recursive function steps down by 2 on every call. What value is printed…
- A function is passed to another function as an argument. What does twice print…
- Two counters are built from the same closure factory. What does the last line…
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