Completed
Scope + mixed functions revision
Questions From This Challenge
Q1mcq1 mark
What is the scope of a variable created inside a function?
Global scope
Local scope ✓
Module scope
File scope
Q2mcq1 mark
A variable defined outside all functions, at the top level of a program, has:
Local scope
Global scope ✓
No scope
Temporary scope
Q3mcq1 mark
What happens when a local variable inside a function has the same name as a global variable?
Python raises an error immediately
Inside the function, the local variable takes precedence; the global variable outside remains unchanged ✓
Both variables merge into one
The global variable is permanently deleted
Q4mcq1 mark
Which keyword must be used inside a function before assigning to a variable, in order to modify that variable in the global scope?
local
global ✓
nonlocal
public
Q5mcq1 mark
Which statement about local variables is true?
They exist only while the function is executing and are not accessible outside it ✓
They can be accessed from anywhere in the program
They are created before the program starts
They replace all global variables automatically
Q6mcq1 mark
What is the term for the region of a program where a variable can be accessed?
Domain
Scope ✓
Range
Namespace boundary
Q7mcq1 mark
If a function reads (but does not assign to) a variable that is not defined locally, from where does Python fetch its value?
It causes an immediate error; variables can never be read this way
From the global scope, if such a variable exists there ✓
Python creates a new local variable automatically set to zero
From a random module
Q8output1 mark
What is the output?
x = 10
def change():
x = 20
print(x)
change()
print(x)
Correct output:
20
10Q9output1 mark
What is the output?
count = 5
def update():
global count
count = count + 1
print(count)
update()
print(count)
Correct output:
6
6Q10output1 mark
What is the output?
total = 100
def show_total():
print(total)
show_total()
Correct output:
100Q11output1 mark
What is the output?
def add_bonus(amount):
amount = amount + 50
print(amount)
value = 200
add_bonus(value)
print(value)
Correct output:
250
200Q12output1 mark
What is the output?
def func_a():
msg = "A"
print(msg)
def func_b():
msg = "B"
print(msg)
func_a()
func_b()
Correct output:
A
BQ13output1 mark
What is the output?
scores = [10, 20]
def add_score(s):
scores.append(s)
add_score(30)
print(scores)
Correct output:
[10, 20, 30]Q14output1 mark
What is the output?
balance = 1000
def withdraw(amount):
global balance
balance = balance - amount
return balance
new_balance = withdraw(200)
print(new_balance, balance)
Correct output:
800 800Q15output1 mark
What is the output?
multiplier = 3
def scale(value, factor=multiplier):
return value * factor
print(scale(5))
print(scale(5, 10))
Correct output:
15
50