Bird
0
0

Why does the following code produce an error?

hard📝 Conceptual Q10 of 15
Python - Variables and Dynamic Typing
Why does the following code produce an error?
def func():
    x = 10
print(x)
Ax is local to func and not accessible outside
Bx is not assigned any value
Cprint syntax is incorrect
DNo error, prints 10
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable scope

    Variable x is defined inside the function func, so it is local to that function.
  2. Step 2: Accessing x outside function

    Trying to print x outside the function causes a NameError because x is not defined in the global scope.
  3. Final Answer:

    x is local to func and not accessible outside -> Option A
  4. Quick Check:

    Local variables are not accessible outside their function [OK]
Quick Trick: Variables inside functions are local by default [OK]
Common Mistakes:
MISTAKES
  • Assuming local variables are global
  • Expecting print to work outside function
  • Ignoring scope rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes