NEW
Font size
WorksheetsQuiz4-CC102-MakScie
Total questions: 40
Worksheet time: 40mins
Which of the following best describes “scope” in Python?
The part of code where a variable is visible
The lifetime of a variable
The type of variable
The memory location of a variable
Which scope applies to variables defined inside a function?
Global
Local
Enclosing
Built-in
Variables declared inside a function are destroyed when the function exits. This refers to:
Scope
Lifetime
Instantiation
Memory mapping
Which keyword allows modification of a global variable inside a function?
nonlocal
global
static
extern
Which of the following is NOT part of the LEGB rule?
nonlocal
global
static
extern
What does the “E” in LEGB stand for?
Environment
External
Enclosing
Execution
Built-in scope contains:
User-defined global variables
Python keywords and functions
Environment variables
Variables imported from other modules
What happens if Python cannot find a variable in any LEGB scope?
It raises SyntaxError
It assigns a default value
It raises NameError
It generates a warning
What will be the output?
x = 10
def test():
x = 20
test()
print(x)
The value of x remains 10 outside the function.
The value of x changes to 20 outside the function.
The value of x is undefined outside the function.
An error occurs due to variable scope.
What will be the output?
x = 5
def sample():
global x
x = 15
sample()
print(x)
A. 5
B. 10
C. 15
D. Error
What keyword is used to modify a variable from an outer (non-global) function?
global
outer
nonlocal
static
What is the output?
def outer():
x = 5
def inner():
print(x)
inner()
outer()
Error
5
None
Undefined
Storage class in Python refers mainly to:
How long a variable exists
A variable’s type
Memory management syntax
External library storage
Python variables are stored:
On the stack only
On the heap only
On both stack and heap
In the CPU register
Which variable’s lifetime lasts until the program ends?
Local
Global
Enclosing
Built-in
What will this code print?
def func():
x = 2
print(x)
A. 2
B. None
C. Error
D. Undefined
A function’s local variables are stored in:
Global namespace
Local namespace
Built-in namespace
Class namespace
Which is TRUE about Python namespaces?
They are dictionaries
They are lists
They are sets
They are static memory blocks
The built-in function that returns current namespace as a dictionary is:
scope()
globals()
namespace()
builtins()
The local namespace of a function can be inspected using:
locals()
globals()
scope()
inspect()
Which of the following refers to a variable defined in a class but outside any method?
Local variable
Class variable
Instance variable
Global variable
Which variable is unique for each object instance?
Class variable
Static variable
Instance variable
Global variable
What is the output?
x = 1
def f():
x = x + 1
print(x)
f()
2
1
Error
None
A global variable can be accessed inside a function without using global keyword if:
It is not modified
It is a constant
It is integer
It is declared first
What do you call a variable first created inside a function but declared global?
Local variable
Enclosing variable
Global variable
Dynamic variable
In nested functions, the nonlocal variable refers to:
Global variable
Local variable of inner function
Local variable of outer function
Built-in variable
What is the output?
def outer():
x = 3
def inner():
nonlocal x
x = 5
inner()
print(x)
outer()
A. 3
B. 5
C. Error
D. None
Which storage characteristic does Python NOT explicitly support like C?
automatic
register
static
All of the above
Global variables declared inside a function using the global keyword belong to:
Local namespace
Enclosing namespace
Global namespace
Built-in namespace
Which statement is TRUE?
Global variables can only be declared at the top of the program
Global variables persist until program termination
Local variables persist until program termination
Nonlocal variables persist permanently
What is shadowing?
Using global inside local
Identical names in different scopes
Using nonlocal on global variables
Removing variable reference
What is the output?
x = 10
def f():
print(x)
def g():
x = 5
f()
g()
10
5
Error
None
Which of the following is the default namespace in Python?
builtins module
user namespace
global namespace
static memory
What type of variable has the shortest lifetime?
Class variable
Global variable
Local variable
Built-in variable
What will this print?
def func(a):
b = a + 2
return b
print(b)
A. 2
B. 3
C. Error
D. Undefined
The lifetime of a local variable begins when:
Program starts
Function is called
Module loads
Class is created
The LEGB rule resolves names in what order?
Local → Built-in → Enclosing → Global
Local → Enclosing → Global → Built-in
Global → Local → Enclosing → Built-in
Built-in → Global → Local → Enclosing
What is the output?
count = 0
def increment():
global count
count += 1
increment()
print(count
A. 0
B. 1
C. Error
D. None
What will happen if a variable is accessed before assignment in local scope?
It prints None
It uses global value
NameError
SyntaxError
Which of the following is the correct way to list all built-in functions in Python?
builtins()
dir(builtins)
builtins.list()
namespace()
