Q.Observe the given Python code carefully: a=20 def convert(a): b=20 a=a+b convert(10) print(a) Select the correct output from the given options: (A) 10 (B) 20 (C) 30 (D) Error
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The output of the code is 20 because the variable a inside the function is local and does not change the global a outside it.
This question tests your understanding of how Python handles variable scope — specifically, the difference between global and local variables. The code defines a global variable a = 20, then defines a function convert(a) that takes a parameter also named a. Inside the function, a new local variable b is set to 20, and then a (the local parameter) is increased by b. The function is called with the argument 10, so inside the function, the local a starts at 10, becomes 30 after adding b, and then the function ends. The crucial point: this change affects only the local a inside the function, not the global a outside.
When the function call finishes, the program moves to the next line: print(a). At this point, a refers to the global variable, which was never modified — it remains 20. The local a that was changed to 30 is gone, as if it never existed outside the function.
If the programmer had intended to modify the global a, they would have used the global keyword inside the function. Without it, any assignment to a inside the function creates or updates a local variable only. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.