PCEP-30-02 · Question #178
v = 1 def fun(): global v v = 2 return v print(v)
The correct answer is B. 1. Option B (1) is correct because fun() is never called - it is only defined. Since the function never executes, the global v declaration and the reassignment v = 2 inside it never run, so v retains its original value of 1 when print(v) is reached. Why the distractors are wrong…
Question
Options
- ANone
- B1
- CThe program will cause an error.
- D2
How the community answered
(36 responses)- A17% (6)
- B72% (26)
- C3% (1)
- D8% (3)
Explanation
Option B (1) is correct because fun() is never called - it is only defined. Since the function never executes, the global v declaration and the reassignment v = 2 inside it never run, so v retains its original value of 1 when print(v) is reached.
Why the distractors are wrong:
- D (2) is the most common trap: readers assume the
globalkeyword andv = 2inside the function take effect just by being defined, but function bodies only execute when called. - A (None) would only be correct if
print(fun())were used and the function returned nothing -fun()does return a value, but again, it's never invoked. - C (error) is incorrect because the code is syntactically valid and runs without exception.
Memory tip: Ask yourself, "Is the function called or just defined?" A function definition (def) is just a blueprint - no code inside it runs until you invoke it with parentheses (e.g., fun()). If you don't see the call, the function's body is invisible to the program flow.
Community Discussion
No community discussion yet for this question.