Free PCEP Variable Scope and the `global` keyword Questions and Answers 1 — Questions and Answers
Question 1: What is the output of the following code? ```python var = 100 def my_func(): var = 50 print(var) my_func() print(var) ```
- 50 100 (Correct answer)
- 100 50
- 50 50
- 100 100
Correct answer: 50 100
Inside `my_func`, `var = 50` creates a new local variable named `var` that shadows the global variable. This local variable is printed (50). The global variable `var` remains unchanged, so the final `print(var)` statement prints its original value (100).
Question 2: What is the output of this code snippet? ```python var = 100 def my_func(): global var var = 50 my_func() print(var) ```
- 100
- 50 (Correct answer)
- An error occurs.
- None
Correct answer: 50
The `global var` statement tells Python that within this function, `var` refers to the globally scoped variable. Therefore, the assignment `var = 50` modifies the global variable, not a local one. The final print statement outputs the new value of the global variable.
Question 3: What is the scope of a variable defined inside a function, without using the `global` keyword?
- Global scope
- Universal scope
- Local scope (Correct answer)
- It has no scope.
Correct answer: Local scope
A variable that is created (assigned a value) inside a function is local to that function. It can only be accessed from within that function and is destroyed when the function finishes executing.
Question 4: What is the output of the following code? ```python value = 1 def change_value(): print(value) change_value() ```
- 1 (Correct answer)
- None
- An UnboundLocalError occurs.
- A NameError occurs.
Correct answer: 1
Functions can read or access global variables without needing the `global` keyword. The `global` keyword is only required when you need to modify or assign a new value to the global variable from within the function.
Question 5: What happens when this code is executed? ```python def my_func(): global new_var new_var = 'Hello' my_func() print(new_var) ```
- A NameError occurs because `new_var` is not defined globally.
- A SyntaxError occurs.
- The code prints 'Hello'. (Correct answer)
- The code runs, but prints nothing.
Correct answer: The code prints 'Hello'.
If you use the `global` keyword for a variable that does not yet exist in the global scope, Python will create that variable in the global scope when it is assigned a value inside the function. Therefore, `new_var` becomes a global variable accessible after the function call.
Question 6: What is the result of running this code? ```python a = 10 def func(a): a = 5 return a print(func(a)) print(a) ```
- 5 10 (Correct answer)
- 5 5
- 10 5
- 10 10
Correct answer: 5 10
The `a` in `func(a)` is a function parameter, which is a local variable. When `func(a)` is called, the value of the global `a` (10) is passed to the local `a`. Inside the function, this local `a` is changed to 5 and returned. The global `a` is never modified.
Question 7: What kind of error will this code produce? ```python count = 0 def increment(): count = count + 1 increment() ```
- NameError
- TypeError
- UnboundLocalError (Correct answer)
- No error, the code runs fine.
Correct answer: UnboundLocalError
This code causes an `UnboundLocalError`. When you assign to a variable in a scope (e.g., `count = ...`), Python treats it as a local variable for the entire scope. Therefore, when it tries to read `count` on the right side of the expression, it sees a local variable that hasn't been assigned a value yet.
What is the output of the following code?
```python
var = 100
def my_func():
var = 50
print(var)
my_func()
print(var)
```