Python Variable Scope Basics Quiz
A 35-question quiz covering local vs global scope, the global keyword, variable shadowing, and best practices for managing state.
Question 1
What is the primary difference between a global variable and a local variable in Python?
Question 2
Analyze the code below. What will be printed when `my_function()` is called?
x = 10
def my_function():
print(x)
my_function()Question 3
What happens if you try to modify a global variable inside a function without using the `global` keyword?
count = 0
def increment():
count += 1
increment()Question 4
Which keyword is used to declare that a variable inside a function refers to a variable in the global scope?
Question 5
Analyze the code below. What is the output?
x = 5
def change_x():
x = 10
print(x)
change_x()
print(x)Question 6
What is 'variable shadowing'?
Question 7
Why is it generally recommended to avoid using global variables for managing state in large programs?
Question 8
Analyze the code below. What is the value of `result`?
def calculate(a):
b = 10
return a + b
result = calculate(5)
print(b)Question 9
What is the correct way to modify a global variable `total` inside a function?
total = 0
def add_to_total(amount):
# What goes here?
total += amountQuestion 10
Analyze the code below. What is the output?
name = "Alice"
def greet():
name = "Bob"
return f"Hello, {name}"
print(greet())
print(name)Question 11
Which of the following best describes the LEGB rule for scope resolution?
Question 12
What happens to local variables when a function finishes executing?
Question 13
Analyze the code below. What is the output?
def outer():
x = "outer"
def inner():
print(x)
inner()
outer()Question 14
Instead of using `global` to modify a variable, what is the preferred functional approach?
Question 15
Analyze the code below. What is the output?
x = 10
def test():
global x
x = 20
test()
print(x)Question 16
Can a function read a global variable without the `global` keyword?
Question 17
Analyze the code below. What is the output?
def my_func():
y = 5
return y
print(my_func())
print(y)Question 18
What is the scope of a variable defined inside a `for` loop in Python (unlike some other languages)?
for i in range(3):
pass
print(i)Question 19
Analyze the code below. What is the output?
x = "global"
def outer():
x = "enclosing"
def inner():
x = "local"
print(x)
inner()
outer()Question 20
Why might a beginner be confused by the following code?
a = [1, 2]
def modify(lst):
lst.append(3)
modify(a)
print(a)Question 21
What is the output of this code?
x = 10
def func(x):
x = 20
print(x)
func(x)
print(x)Question 22
Which of the following is a 'Built-in' scope example?
Question 23
Analyze the code below. What is the output?
def my_func():
global z
z = 100
my_func()
print(z)Question 24
What is the danger of shadowing a built-in function name?
def print(s):
return s
# Later in code...
print("Hello")Question 25
Analyze the code below. What is the output?
val = 1
def func():
val = 2
def inner():
nonlocal val
val = 3
inner()
print(val)
func()Question 26
If a variable is not found in Local, Enclosing, or Global scope, where does Python look last?
Question 27
Analyze the code below. What is the output?
x = 5
if True:
x = 10
print(x)Question 28
What is the best practice for passing data into a function?
Question 29
Analyze the code below. What is the output?
count = 10
def demo():
print(count)
count = 20
demo()Question 30
How does Python determine if a variable is local?
Question 31
Analyze the code below. What is the output?
def func(lst=[]):
lst.append(1)
return lst
print(func())
print(func())Question 32
What is the output of `globals() == locals()` when run at the top level of a script?
Question 33
Analyze the code below. What is the output?
x = 100
def func():
x = 200
def nested():
global x
x = 300
nested()
print(x)
func()
print(x)Question 34
Which statement is true regarding variable scope and memory management?
Question 35
Analyze the code below. What is the output?
i = 5
def loop_test():
for i in range(3):
pass
print(i)
loop_test()
print(i)