Question
In Python, what will be the output of the following code
snippet, considering scope rules? x = 5               def func():    x = 10    def inner():        nonlocal x        x += 1        return x    return inner()  print(func())Solution
The code demonstrates the use of the nonlocal keyword in Python, which allows a variable defined in an enclosing (but not global) scope to be modified. Here’s a breakdown:
- The variable x = 5 is declared globally.
- Inside the func() function, a local x = 10 is declared.
- Within the nested inner() function, the nonlocal x statement is used. This tells Python that x refers to the x variable in the enclosing func() scope (not the global scope).
- The x is incremented by 1 (x += 1), updating it to 11.
- The inner() function returns x, and func() also returns the result from inner(). Hence, the final output is 11.
- Option A (6): This assumes that nonlocal affects the global x. However, nonlocal only modifies variables in the closest enclosing non-global scope. Thus, the global x = 5 remains unchanged.
- Option C (10): This would be true if no modification occurred to the local x inside the inner() function. However, x is incremented by 1 due to nonlocal.
- Option D (UnboundLocalError): This error would occur if nonlocal was omitted and x was incremented without declaration, as Python would not recognize it as being defined in the current scope.
- Option E (None): This would result if func() or inner() explicitly returned None. Since the function returns x, this is incorrect.
One bag contains 5 yellow hats and 4pink hats. Another bag contains 3 yellow hats and 7pink hats. If one hat is drawn from each bag, what is the probabi...
- A basket contains x black balls, (x + 3) white balls and (x + 6) yellow balls. If probability of picking a white ball is 1/11 more than that of picking a b...
The probability of selecting a rotten egg randomly from a basket of 800 eggs is 0.26. What is the number of rotten eggs in the basket?
- A bag contains ‘x’ blue balls and 5 yellow balls. If the probability of getting a blue ball is 2/3, then find the probability of getting different colo...
The probability of selecting a rotten apple randomly from a heap of 150 apples is 0.28. What is the number of rotten apples in the heap?
A bag contains 7 orange balls, 8 red balls and 5 black balls. Three balls are drawn at random. Find the probability that at least two balls are red.
A basket contains ‘x’ red apples and 12 green apples. If the probability of selecting a red apple is 2/5, then find the probability that two apples ...
- 18 defective pieces of keyboards got mixed with 165 pieces of non-defective keyboards. A customer comes and buys one keyboard. Find the probability of the ...
In a purse, there are 3 two-rupee coins and 4 five-rupee coins. If you randomly select 4 coins from the purse, what is the probability that you will cho...
A bag contains 8 green balls, 5 yellow balls and 3 blue balls. Two balls are drawn simultaneously. Find the probability that both the balls are of same ...