🎩 Master Quiz: Global vs. Local
20 Questions to test your Scope management skills.
Green = Correct, Red = Incorrect.
In the Office Analogy, what represents a Local Variable?
💡 Explanation
Your Private Notebook.
Local variables are like notes inside your private cabin (function). People outside cannot see or access them.
Where are Global Variables created?
💡 Explanation
At the top level.
Variables declared outside of any function are Global and accessible everywhere in the script.
Can a function read (print) a Global variable without special keywords?
💡 Explanation
Yes.
Functions can read global variables by default. The issue only arises when you try to modify them.
What happens if you type x = 10 inside a function, but x already exists globally?
💡 Explanation
It creates a new Local x.
Python assumes any assignment inside a function creates a local variable, leaving the global one untouched (shadowing).
Why are excessive Global Variables considered a “Code Smell”?
💡 Explanation
Tight Coupling.
If 10 functions rely on one global variable and one changes it unexpectedly, all 10 functions might break.
Which keyword allows you to modify a global variable inside a function?
💡 Explanation
global
You must explicitly state global x inside the function before assigning a new value to it.
When does a Local Variable “die” (get removed from memory)?
💡 Explanation
When the function finishes.
Local variables are temporary. This makes them memory efficient.
What is the order in which Python looks for variables?
💡 Explanation
LEGB Rule.
Local first. If not found, check Enclosing. If not found, check Global. Finally, check Built-in.
If logs = [] is global, can a function do logs.append("Error") without the global keyword?
💡 Explanation
Yes.
You are modifying the contents of the object, not assigning a new object to the variable name. Therefore, global is not needed.
What causes UnboundLocalError: local variable referenced before assignment?
💡 Explanation
Referencing before assignment.
If Python sees x = 5 anywhere in the function, it treats x as local for the entire function. If you try to print x before that line, it crashes.
What is the best practice naming convention for Global Constants?
💡 Explanation
ALL_CAPS.
This signals to other developers: “This is a global constant. Do not change it.” Example: API_URL.
If Function A has x=10 and Function B has x=20, do they interfere?
💡 Explanation
No, they are isolated.
Local variables are like private notebooks. The x in Function A has no relation to the x in Function B.
Which of these is a good candidate for a Local Variable?
💡 Explanation
A loop counter.
Temporary values used for calculations or iteration should always be local.
Where should sensitive data like a user’s password be stored?
💡 Explanation
Local Variable.
Local variables are cleared from memory immediately after use. Globals persist, increasing the risk of memory dumps exposing secrets.
x = 5
def test():
print(x)
test()What is the output?
💡 Explanation
5.
The function tries to read x. It doesn’t find it locally, so it looks up to the Global scope and finds 5.
x = 5
def test():
x = 10
print(x)
test()
print(x)What is the output?
💡 Explanation
10 then 5.
Inside the function, x is 10 (Local). Outside, x remains 5 (Global) because the function created a new variable instead of modifying the global one.
What is a “Zombie Variable”?
💡 Explanation
A global that changes unexpectedly.
This occurs when you lose track of which function is modifying a global variable, causing values to “come back from the dead” or change randomly.
Which type of variable relies on Garbage Collection happening “sooner”?
💡 Explanation
Local variables.
Since they go out of scope as soon as the function returns, they are candidates for immediate cleanup, freeing up RAM.
Can the main script access a variable created inside a function?
💡 Explanation
No.
What happens in the function, stays in the function. The main script cannot see inside local scopes.
If you want to read a global variable, do you NEED to write global x?
💡 Explanation
No.
You only need the global keyword if you intend to assign (write) a new value to it.