Learn with Yasir

Share Your Feedback

Python Local and Global Variables MCQs – Multiple Choice Questions for Practice

Test your understanding of local and global variables in Python with these multiple choice questions. Practice concepts like variable scope, usage, and common mistakes with beginner-friendly MCQs and detailed answers. Ideal for students and Python learners.

Topic: local-global


📝 Multiple Choice Questions

🟢 Beginner

Q1. Which keyword allows you to modify a global variable inside a function in Python?

  • 🟢 A. local
  • 🔵 B. global
  • 🟠 C. static
  • 🔴 D. constant
Answer

global

The global keyword makes the variable refer to the global scope.


Q2. What is a local variable in Python?

  • 🟢 A. A variable defined outside any function
  • 🔵 B. A variable defined inside a function
  • 🟠 C. A variable with uppercase letters
  • 🔴 D. A built-in Python variable
Answer

A variable defined inside a function

Local variables are declared inside functions and only exist while the function runs.


Q3. Which of the following is considered a constant in Python by convention?

  • 🟢 A. pi
  • 🔵 B. radius
  • 🟠 C. PI
  • 🔴 D. var_name
Answer

PI

Variables written in uppercase are treated as constants by convention in Python.


Q4. Where can a global variable be accessed?

  • 🟢 A. Inside any function
  • 🔵 B. Outside any function
  • 🟠 C. Both inside and outside functions
  • 🔴 D. Only inside a class
Answer

Both inside and outside functions

Global variables are accessible throughout the program.


Q5. A global variable is defined:

  • 🟢 A. Inside the body of a function
  • 🔵 B. Inside a loop
  • 🟠 C. At the top level of a module
  • 🔴 D. Only inside classes
Answer

At the top level of a module

A variable declared outside all functions is global.


🟡 Intermediate

Q1. What is the output of the following code?

x = 10
def my_function():
    x = 5
    return x
print(my_function(), x)
  • 🟢 A. (5, 10)
  • 🔵 B. (10, 10)
  • 🟠 C. (5, 5)
  • 🔴 D. (10, 5)
Answer

(5, 10)

Inside the function, a local variable x shadows the global variable. So my_function() returns 5 and the global x remains 10.


Q2. What is printed by the following code?

x = 15
def foo():
    global x
    x = x + 5
foo()
print(x)
  • 🟢 A. 15
  • 🔵 B. 5
  • 🟠 C. 20
  • 🔴 D. 10
Answer

20

The global keyword allows the function to modify the global variable x. x becomes 20.


Q3. What will be the value of y after the code is executed?

Y_CONSTANT = 50
def change():
    y = 10
    return y
y = change()
print(y)
  • 🟢 A. 50
  • 🔵 B. 10
  • 🟠 C. 60
  • 🔴 D. 0
Answer

10

The constant Y_CONSTANT remains 50 and is not used in change(). y gets the local value 10 returned from change().


Q4. What is the output of the following code?

PI = 3.14
def area(radius):
  return PI * radius * radius
PI = 3
print(area(2))
  • 🟢 A. 12.56
  • 🔵 B. 9
  • 🟠 C. 6
  • 🔴 D. 4
Answer

12.56

area() uses the value 3 for PI when executing, even though PI is conventionally a constant.


Q5. What will be printed?

count = 5
def show():
    count = 3
    print(count)
show()
print(count)
  • 🟢 A. 3 5
  • 🔵 B. 5 3
  • 🟠 C. 3 3
  • 🔴 D. 5 5
Answer

3 5

The function defines a new local variable count=3; the global count remains 5.


Q6. What is the output of the code?

A = 7
def test():
    return A * 2
print(test())
  • 🟢 A. 7
  • 🔵 B. 14
  • 🟠 C. 0
  • 🔴 D. TypeError
Answer

14

A is global and can be used inside the function for computing the result.


Q7. What happens if you try to assign a value to a global variable inside a function without using the 'global' keyword?

x = 1
def func():
    x = x + 1
    return x
print(func())
  • 🟢 A. 2
  • 🔵 B. 1
  • 🟠 C. UnboundLocalError
  • 🔴 D. True
Answer

UnboundLocalError

Python treats x as a local variable inside the function and raises an UnboundLocalError.


Q8. What will the following code print?

MAX_SIZE = 100
def reset():
    global MAX_SIZE
    MAX_SIZE = 50
reset()
print(MAX_SIZE)
  • 🟢 A. 100
  • 🔵 B. 0
  • 🟠 C. 50
  • 🔴 D. None
Answer

50

Using global inside the function modifies the global constant MAX_SIZE.


Q9. In Python, constants are:

  • 🟢 A. Enforced by the interpreter
  • 🔵 B. Declared using const keyword
  • 🟠 C. Only a naming convention
  • 🔴 D. Not allowed in Python
Answer

Only a naming convention

Python does not enforce constants; they are written in uppercase by convention.


Q10. What is the output?

def example():
    a = 6
    def inner():
        nonlocal a
        a += 2
        return a
    return inner()
print(example())
  • 🟢 A. 6
  • 🔵 B. 8
  • 🟠 C. 4
  • 🔴 D. Error
Answer

8

nonlocal is used to modify the variable from the parent function scope.


Q11. What does the 'nonlocal' keyword do?

  • 🟢 A. Defines a global variable
  • 🔵 B. Defines a local variable
  • 🟠 C. Refers to a variable in an outer function
  • 🔴 D. Creates a constant
Answer

Refers to a variable in an outer function

The nonlocal keyword refers to variables in the nearest enclosing scope that is not global.


Q12. What will be printed?

RATE = 5
def calc(val):
    return val + RATE
print(calc(10))
  • 🟢 A. 15
  • 🔵 B. 10
  • 🟠 C. 5
  • 🔴 D. 0
Answer

15

RATE is treated as a constant and is available inside the function.


Q13. What is the result of executing the following code?

x = 2
def add():
    return x + 3
x = 7
print(add())
  • 🟢 A. 5
  • 🔵 B. 10
  • 🟠 C. 2
  • 🔴 D. 3
Answer

10

The value of x used is the updated global value 7.


Q14. What will be the value of z after the code runs?

z = 4
def test():
    z = 6
    return z
test()
print(z)
  • 🟢 A. 4
  • 🔵 B. 6
  • 🟠 C. 10
  • 🔴 D. 0
Answer

4

z inside the function is local and does not affect the global z.


Q15. Which of the following will correctly update the global variable 'count'?

count = 5
def update():
    # fill the blank
    count = count + 1
update()
print(count)
  • 🟢 A. global count
  • 🔵 B. local count
  • 🟠 C. nonlocal count
  • 🔴 D. static count
Answer

global count

Using the global keyword is required to update the global variable inside the function.


🔴 Advanced

Explore More Topics

📘 Learn Python

Tutorials, Roadmaps, Bootcamps & Visualization Projects

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules