MCQ Hive stays free thanks to ads. Please disable your ad blocker for this site so content and features keep working.
Fundamental Programming
Variables & Scope
Beginner
Q
What is the output of this Python code?
x = 10
def f():
print(x)
x = 5
f()
AUnboundLocalError is raised
BNone
C10
D5
Correct Answer:
A. UnboundLocalError is raised
Explanation:
Because x is assigned somewhere in f, Python treats x as local throughout the function. print(x) runs before that local is assigned, which raises UnboundLocalError.
ALSO KNOW (related points):
β’ What is the output of this C++ code?: int f(){ static int c = 0; return ++c; }; int main(){ f(); f(); cout