Python Iterators and Generators Quiz: 20 MCQs with Answers

Python Scope and Namespace Interactive Quiz

Test your knowledge of Python variable scope and namespaces using local variables, global variables, the global keyword, the nonlocal keyword, and the LEGB rule.

What Are Scope and Namespace in Python?

In Python, scope determines where a variable can be accessed in a program, while a namespace is a collection that maps names to objects. Understanding scope helps you write functions and programs without unexpected variable conflicts.

This quiz covers:

  • Python variable scope
  • Local and global variables
  • The global keyword
  • The nonlocal keyword
  • Nested functions
  • The LEGB rule
  • Namespaces
  • Variable lookup
  • Practical scope examples
  • Common scope-related errors

Take this quiz to test your Python Scope and Namespace knowledge and improve your understanding of how Python finds and manages variables.

📌 Quick Summary

Python Scope and Namespace Quiz: Test your knowledge with 20 interactive multiple-choice questions covering local scope, global scope, nested functions, namespaces, the global and nonlocal keywords, and the LEGB rule.

  • 📝 20 Python Scope & Namespace MCQs
  • ⏱ 160-second interactive quiz
  • 💡 Answer explanations included
  • Suitable for Python beginners
  • 💻 Includes output-based questions

⏱ Time Left: 160 seconds

Q1. What does variable scope determine in Python?




Q2. Which type of variable is normally created inside a function?




Q3. What is the output?

x = 10

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

test()
print(x)



Q4. A variable created outside all functions is generally considered what type of variable?




Q5. Which keyword is used to modify a global variable from inside a function?




Q6. What is the output?

x = 10

def change():
global x
x = 50

change()
print(x)



Q7. What is the purpose of the global keyword?




Q8. What is the output?

x = 5

def test():
print(x)

test()



Q9. What does the LEGB rule describe?




Q10. In the LEGB rule, what does the letter L represent?




Q11. In the LEGB rule, what does the letter E represent?




Q12. In the LEGB rule, what does the letter G represent?




Q13. What is the output?

def outer(): x = 10
def inner():
    print(x)

inner()


outer()



Q14. Which keyword allows a nested function to modify a variable from its enclosing function?




Q15. What is the output?

def outer(): x = 10
def inner():
    nonlocal x
    x = 20

inner()
print(x)


outer()



Q16. What is a namespace in Python?




Q17. What happens when a local variable has the same name as a global variable?

x = 10

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

test()



Q18. What happens in this code?

x = 10

def test():
x = x + 1
print(x)

test()



Q19. Which scope is searched last in the LEGB lookup order?




Q20. Which statement about Python variable scope is correct?







Why Is Python Variable Scope Important?

Ans: Understanding variable scope is important because it determines where variables can be accessed and modified. Python uses different scopes to organize variables and prevent unnecessary conflicts between names.

  • Understand where variables can be accessed
  • Avoid unexpected variable changes
  • Work effectively with functions
  • Understand nested functions
  • Use global and nonlocal correctly
  • Understand Python's LEGB name-resolution rule
  • Debug variable-related errors more effectively


❓ Frequently Asked Questions About Python Scope and Namespace

What is variable scope in Python?

Variable scope refers to the part of a Python program where a particular variable name can be accessed. Python commonly works with local, enclosing, global, and built-in scopes.

What is a local variable in Python?

A local variable is generally created inside a function and can normally be accessed only within that function.

What is a global variable in Python?

A global variable is generally defined outside functions at the module level. It can be read from functions when it is visible in the relevant scope.

What is the global keyword in Python?

The global keyword tells Python that a variable assigned inside a function refers to a global variable rather than creating a new local variable.

What is the nonlocal keyword in Python?

The nonlocal keyword is used inside a nested function when you want to modify a variable belonging to an enclosing function.

What is the LEGB rule in Python?

LEGB stands for Local, Enclosing, Global, and Built-in. It describes the order Python follows when searching for a name.

What is a namespace in Python?

A namespace is a mapping between names and the objects they refer to. Python uses namespaces to keep track of variables, functions, classes, and other objects.


Continue Learning and Testing Your Python Knowledge Step by Step:



Featured Post

Python Iterators and Generators Quiz: 20 MCQs with Answers

Python Scope and Namespace Interactive Quiz Test your knowledge of Python variable scope and namespaces using local variables, global...

Popular Posts