Skip to main content

Python Global vs. Local Variables Quiz

ยท 6 min read

Progress

Score: 0/20

๐ŸŽฉ Master Quiz: Global vs. Local

20 Questions to test your Scope management skills. Green = Correct, Red = Incorrect.

BeginnerAnalogy

In the Office Analogy, what represents a Local Variable?โ€‹

  • **A) The Public Notice Board**
  • **B) The Building Security Guard**
  • **C) Your Private Notebook**
  • **D) The Elevator**

๐Ÿ’ก Explanationโ€‹

Your Private Notebook.

Local variables are like notes inside your private cabin (function). People outside cannot see or access them.

BeginnerScope

Where are Global Variables created?โ€‹

  • **A) Inside a function**
  • **B) At the top level (Main script)**
  • **C) Inside a class only**
  • **D) In a separate file only**

๐Ÿ’ก Explanationโ€‹

At the top level.

Variables declared outside of any function are Global and accessible everywhere in the script.

IntermediateAccess Rule

Can a function read (print) a Global variable without special keywords?โ€‹

  • **A) Yes**
  • **B) No, it raises an error**
  • **C) Only if the variable is an integer**
  • **D) Only if the function is empty**

๐Ÿ’ก Explanationโ€‹

Yes.

Functions can read global variables by default. The issue only arises when you try to modify them.

IntermediateBehavior

What happens if you type x = 10 inside a function, but x already exists globally?โ€‹

  • **A) It overwrites the Global x**
  • **B) It creates a new Local x**
  • **C) It raises a SyntaxError**
  • **D) It deletes the Global x**

๐Ÿ’ก Explanationโ€‹

It creates a new Local x.

Python assumes any assignment inside a function creates a local variable, leaving the global one untouched (shadowing).

ArchitectCode Smell

Why are excessive Global Variables considered a "Code Smell"?โ€‹

  • **A) They use too much CPU**
  • **B) They are too slow to access**
  • **C) They cause Tight Coupling (Debugging Nightmare)**
  • **D) They cannot be deleted**

๐Ÿ’ก Explanationโ€‹

Tight Coupling.

If 10 functions rely on one global variable and one changes it unexpectedly, all 10 functions might break.

AdvancedKeywords

Which keyword allows you to modify a global variable inside a function?โ€‹

  • **A) global``**
  • **B) static``**
  • **C) external``**
  • **D) public``**

๐Ÿ’ก Explanationโ€‹

global

You must explicitly state global x inside the function before assigning a new value to it.

IntermediateLifetime

When does a Local Variable "die" (get removed from memory)?โ€‹

  • **A) When the program ends**
  • **B) When the function finishes running**
  • **C) Every 5 minutes**
  • **D) Never**

๐Ÿ’ก Explanationโ€‹

When the function finishes.

Local variables are temporary. This makes them memory efficient.

ArchitectLEGB Rule

What is the order in which Python looks for variables?โ€‹

  • **A) Global -> Local -> Built-in**
  • **B) Built-in -> Global -> Local**
  • **C) Local -> Enclosing -> Global -> Built-in**
  • **D) Random Order**

๐Ÿ’ก Explanationโ€‹

LEGB Rule.

Local first. If not found, check Enclosing. If not found, check Global. Finally, check Built-in.

AdvancedMutable Globals

If logs = [] is global, can a function do logs.append("Error") without the global keyword?โ€‹

  • **A) Yes**
  • **B) No**
  • **C) Only in Python 2**
  • **D) Only if the list is empty**

๐Ÿ’ก Explanationโ€‹

Yes.

You are modifying the contents of the object, not assigning a new object to the variable name. Therefore, global is not needed.

AdvancedError Handling

What causes UnboundLocalError: local variable referenced before assignment?โ€‹

  • **A) The variable name is too long**
  • **B) Trying to print a variable, then assigning it later in the function**
  • **C) Importing a missing module**
  • **D) Using a global variable correctly**

๐Ÿ’ก 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.

ArchitectNaming Convention

What is the best practice naming convention for Global Constants?โ€‹

  • **A) camelCase (myConstant)**
  • **B) snake_case (my_constant)**
  • **C) ALL_CAPS (MY_CONSTANT)**
  • **D) PascalCase (MyConstant)**

๐Ÿ’ก Explanationโ€‹

ALL_CAPS.

This signals to other developers: "This is a global constant. Do not change it." Example: API_URL.

BeginnerIsolation

If Function A has x=10 and Function B has x=20, do they interfere?โ€‹

  • **A) Yes, they overwrite each other**
  • **B) No, local variables are isolated**
  • **C) Only if they run at the same time**
  • **D) Yes, if they are in the same file**

๐Ÿ’ก 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.

IntermediateUse Cases

Which of these is a good candidate for a Local Variable?โ€‹

  • **A) A loop counter (i)**
  • **B) The Database Port**
  • **C) The Application Name**
  • **D) The API Key**

๐Ÿ’ก Explanationโ€‹

A loop counter.

Temporary values used for calculations or iteration should always be local.

ArchitectSecurity

Where should sensitive data like a user's password be stored?โ€‹

  • **A) Global Variable**
  • **B) Local Variable**
  • **C) In a text file**
  • **D) In the function name**

๐Ÿ’ก Explanationโ€‹

Local Variable.

Local variables are cleared from memory immediately after use. Globals persist, increasing the risk of memory dumps exposing secrets.

IntermediateCode Analysis

x = 5
def test():
print(x)
test()

What is the output?โ€‹

  • **A) 5**
  • **B) Error: UnboundLocalError**
  • **C) None**
  • **D) 0**

๐Ÿ’ก 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.

IntermediateCode Analysis

x = 5
def test():
x = 10
print(x)
test()
print(x)

What is the output?โ€‹

  • **A) 10 then 10**
  • **B) 5 then 5**
  • **C) 5 then 10**
  • **D) 10 then 5**

๐Ÿ’ก 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.

AdvancedZombie Variables

What is a "Zombie Variable"?โ€‹

  • **A) A deleted variable**
  • **B) A variable named 'zombie'**
  • **C) A global variable that changes unexpectedly**
  • **D) A constant**

๐Ÿ’ก 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.

ArchitectMemory

Which type of variable relies on Garbage Collection happening "sooner"?โ€‹

  • **A) Global**
  • **B) Local**
  • **C) Both are same**
  • **D) Constants**

๐Ÿ’ก Explanationโ€‹

Local variables.

Since they go out of scope as soon as the function returns, they are candidates for immediate cleanup, freeing up RAM.

BeginnerAccess

Can the main script access a variable created inside a function?โ€‹

  • **A) Yes, always**
  • **B) No, it is out of scope**
  • **C) Yes, if using Python 3**
  • **D) Yes, if the function is void**

๐Ÿ’ก Explanationโ€‹

No.

What happens in the function, stays in the function. The main script cannot see inside local scopes.

IntermediateExplicit Declaration

If you want to read a global variable, do you NEED to write global x?โ€‹

  • **A) Yes**
  • **B) No**
  • **C) Only for lists**
  • **D) Only for strings**

๐Ÿ’ก Explanationโ€‹

No.

You only need the global keyword if you intend to assign (write) a new value to it.

๐ŸŽ‰ Quiz Complete!โ€‹

Your Final Score:

0/20 Novice

Keep practicing to reach Architect level!

  • **โ†บ Retake Quiz**