Loading

Quipoin Menu

Learn • Practice • Grow

python / Scope and Global
mcq
Direction: Choose the correct option

Q1.

What is the scope of a variable defined inside a function?
A. Enclosing
B. Global
C. Local
D. Built-in
Direction: Choose the correct option

Q2.

How do you modify a global variable inside a function?
A. Use `nonlocal` keyword
B. Use `globals()` function
C. Use `global` keyword
D. Use `global` keyword
Direction: Choose the correct option

Q3.

What is the output of `x=5; def f(): x=10; print(x); f(); print(x)`?
A. 10 and 10
B. 10 and 5
C. 5 and 5
D. Error
Direction: Choose the correct option

Q4.

What does `nonlocal` do?
A. Creates a new variable
B. Modifies a variable in the nearest enclosing scope (not global)
C. Declares a variable as local
D. Modifies a global variable
Direction: Choose the correct option

Q5.

Given nested function, how do you access an outer function's variable?
A. Use `global`
B. Use `outer`
C. Use `nonlocal`
D. It's automatically accessible
Direction: Choose the correct option

Q6.

What will `def f(): print(x); x=10` do?
A. Prints 10
B. UnboundLocalError: local variable 'x' referenced before assignment
C. Prints global x
D. Works fine
Direction: Choose the correct option

Q7.

What is the built-in scope?
A. Global variables
B. Local variables
C. User-defined functions
D. Predefined names like `print`, `len`
Direction: Choose the correct option

Q8.

How can you see all global variables?
A. vars()
B. dir()
C. locals()
D. globals()
Direction: Choose the correct option

Q9.

What is the LEGB rule?
A. Local, External, Global, Base
B. Local, Enclosing, Global, Built-in
C. List, Enumerate, Global, Built-in
D. Local, Enclosing, Global, Built-in
Direction: Choose the correct option

Q10.

If a variable is defined inside a function, can it be accessed outside?
A. Yes, automatically
B. Yes, globally
C. Only if declared global
D. No, unless returned