Python Variable Scope Basics Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering local vs global scope, the global keyword, variable shadowing, and best practices for managing state.

35 Questions
~70 minutes
1

Question 1

What is the primary difference between a global variable and a local variable in Python?

A
Global variables are accessible throughout the program, while local variables are only accessible within the function where they are defined.
B
Global variables are defined inside functions, while local variables are defined outside.
C
Global variables cannot be modified, whereas local variables can.
D
Local variables are stored in the heap, while global variables are stored in the stack.
2

Question 2

Analyze the code below. What will be printed when `my_function()` is called?

python
x = 10
def my_function():
    print(x)

my_function()
A
10
B
None
C
Error: x is not defined
D
0
3

Question 3

What happens if you try to modify a global variable inside a function without using the `global` keyword?

python
count = 0
def increment():
    count += 1
increment()
A
UnboundLocalError
B
The global count becomes 1
C
A new local variable count is created with value 1
D
SyntaxError
4

Question 4

Which keyword is used to declare that a variable inside a function refers to a variable in the global scope?

A
global
B
outer
C
extern
D
super
5

Question 5

Analyze the code below. What is the output?

python
x = 5
def change_x():
    x = 10
    print(x)

change_x()
print(x)
A
10 then 5
B
10 then 10
C
5 then 5
D
5 then 10
6

Question 6

What is 'variable shadowing'?

A
When a local variable has the same name as a global variable, hiding the global one within that scope.
B
When a variable is deleted from memory.
C
When a variable is made private.
D
When a global variable overwrites a local one.
7

Question 7

Why is it generally recommended to avoid using global variables for managing state in large programs?

A
They make code harder to debug and test because functions become dependent on external state.
B
They are slower to access than local variables.
C
They consume more memory.
D
Python limits the number of global variables you can have.
8

Question 8

Analyze the code below. What is the value of `result`?

python
def calculate(a):
    b = 10
    return a + b

result = calculate(5)
print(b)
A
NameError: name 'b' is not defined
B
10
C
15
D
None
9

Question 9

What is the correct way to modify a global variable `total` inside a function?

python
total = 0
def add_to_total(amount):
    # What goes here?
    total += amount
A
global total
B
use total
C
extern total
D
Nothing needed
10

Question 10

Analyze the code below. What is the output?

python
name = "Alice"
def greet():
    name = "Bob"
    return f"Hello, {name}"

print(greet())
print(name)
A
Hello, Bob then Alice
B
Hello, Bob then Bob
C
Hello, Alice then Alice
D
Hello, Alice then Bob
11

Question 11

Which of the following best describes the LEGB rule for scope resolution?

A
Local, Enclosing, Global, Built-in
B
Local, External, Global, Binary
C
Loop, Exception, Global, Base
D
Last, Early, Global, Basic
12

Question 12

What happens to local variables when a function finishes executing?

A
They are destroyed and their memory is reclaimed.
B
They are moved to the global scope.
C
They are saved to a file.
D
They persist until the program ends.
13

Question 13

Analyze the code below. What is the output?

python
def outer():
    x = "outer"
    def inner():
        print(x)
    inner()

outer()
A
outer
B
None
C
Error
D
inner
14

Question 14

Instead of using `global` to modify a variable, what is the preferred functional approach?

A
Pass the variable as an argument and return the modified value.
B
Use a class with static methods.
C
Write the variable to a file.
D
Use a dictionary as a global container.
15

Question 15

Analyze the code below. What is the output?

python
x = 10
def test():
    global x
    x = 20

test()
print(x)
A
20
B
10
C
Error
D
None
16

Question 16

Can a function read a global variable without the `global` keyword?

A
Yes, read access is allowed by default.
B
No, you always need the global keyword.
C
Only if the variable is an integer.
D
Only if the function has no arguments.
17

Question 17

Analyze the code below. What is the output?

python
def my_func():
    y = 5
    return y

print(my_func())
print(y)
A
5 then Error
B
5 then 5
C
Error then 5
D
None then None
18

Question 18

What is the scope of a variable defined inside a `for` loop in Python (unlike some other languages)?

python
for i in range(3):
    pass
print(i)
A
It leaks into the enclosing function or global scope.
B
It is strictly local to the loop block.
C
It is undefined outside the loop.
D
It becomes a global constant.
19

Question 19

Analyze the code below. What is the output?

python
x = "global"
def outer():
    x = "enclosing"
    def inner():
        x = "local"
        print(x)
    inner()

outer()
A
local
B
enclosing
C
global
D
Error
20

Question 20

Why might a beginner be confused by the following code?

python
a = [1, 2]
def modify(lst):
    lst.append(3)

modify(a)
print(a)
A
Because the list is modified globally even without the global keyword.
B
Because it throws an error.
C
Because the list remains [1, 2].
D
Because lists are immutable.
21

Question 21

What is the output of this code?

python
x = 10
def func(x):
    x = 20
    print(x)

func(x)
print(x)
A
20 then 10
B
20 then 20
C
10 then 10
D
10 then 20
22

Question 22

Which of the following is a 'Built-in' scope example?

A
Functions like len(), print(), and range().
B
Variables defined in the main script.
C
Variables imported from a user module.
D
Variables inside a class.
23

Question 23

Analyze the code below. What is the output?

python
def my_func():
    global z
    z = 100

my_func()
print(z)
A
100
B
Error: z is not defined
C
None
D
0
24

Question 24

What is the danger of shadowing a built-in function name?

python
def print(s):
    return s
# Later in code...
print("Hello")
A
You lose access to the original built-in functionality.
B
It causes a SyntaxError.
C
Python automatically renames your function.
D
Nothing, Python handles it intelligently.
25

Question 25

Analyze the code below. What is the output?

python
val = 1
def func():
    val = 2
    def inner():
        nonlocal val
        val = 3
    inner()
    print(val)

func()
A
3
B
2
C
1
D
Error
26

Question 26

If a variable is not found in Local, Enclosing, or Global scope, where does Python look last?

A
Built-in scope
B
It stops and raises an error immediately.
C
It looks in the file system.
D
It looks in the parent class.
27

Question 27

Analyze the code below. What is the output?

python
x = 5
if True:
    x = 10
print(x)
A
10
B
5
C
Error
D
None
28

Question 28

What is the best practice for passing data into a function?

A
Use arguments (parameters).
B
Use global variables.
C
Use a config file.
D
Use environment variables.
29

Question 29

Analyze the code below. What is the output?

python
count = 10
def demo():
    print(count)
    count = 20

demo()
A
UnboundLocalError
B
10
C
10 then 20
D
20
30

Question 30

How does Python determine if a variable is local?

A
If it is assigned a value anywhere within the function.
B
If it is declared with `var`.
C
If it is passed as an argument only.
D
If it is not in the global scope.
31

Question 31

Analyze the code below. What is the output?

python
def func(lst=[]):
    lst.append(1)
    return lst

print(func())
print(func())
A
[1] then [1, 1]
B
[1] then [1]
C
[1] then [2]
D
Error
32

Question 32

What is the output of `globals() == locals()` when run at the top level of a script?

A
True
B
False
C
Error
D
None
33

Question 33

Analyze the code below. What is the output?

python
x = 100
def func():
    x = 200
    def nested():
        global x
        x = 300
    nested()
    print(x)

func()
print(x)
A
200 then 300
B
300 then 300
C
200 then 100
D
300 then 100
34

Question 34

Which statement is true regarding variable scope and memory management?

A
Limiting variable scope helps garbage collection by allowing memory to be freed sooner.
B
Global variables are always garbage collected immediately.
C
Local variables consume more memory than global ones.
D
Scope has no impact on memory usage.
35

Question 35

Analyze the code below. What is the output?

python
i = 5
def loop_test():
    for i in range(3):
        pass
    print(i)

loop_test()
print(i)
A
2 then 5
B
2 then 2
C
3 then 5
D
5 then 5

QUIZZES IN Python