Return to Dashboard

Module 19: Scope

Answer the following 10 questions about Scope. You need to score at least 6/10 to pass. You can retake this check as many times as you need.

1. What is a 'local variable' in Python?

2. What is a 'global variable' in Python?

3. Consider the following code:

message = "Hello"

def greet():
    message = "Hi"
    print(message)

greet()
print(message)
What will be printed?

4. Where can a variable defined *inside* a function (without using `global`) be accessed?

5. Why is understanding scope important in programming?

6. If a function needs to modify a global variable, what keyword might it need to use *inside* the function before assigning a new value to that variable?

7. Consider this code:

count = 0

def increment():
    count = count + 1 # Attempts to modify 'count'
    print(count)

increment()
What is the most likely outcome when running this code?

8. Can a function access a global variable without using the `global` keyword?

9. What is the scope of a variable defined in a `for` loop statement (like `i` in `for i in range(5):`) within a function?

10. If you define a variable inside a function, and then the function finishes executing, what happens to that local variable?