What will be the output of the following Python code?def fun(): x=15 print(x)x=12fun()Options: Pick one correct answer from belowError151215 12
Question
What will be the output of the following Python code?def fun(): x=15 print(x)x=12fun()Options: Pick one correct answer from belowError151215 12
Solution
The output of the given Python code will be 15.
Here's the step by step explanation:
- The variable
xis first set to 12. - The function
fun()is then called. - Inside the function
fun(), a new local variablexis defined and set to 15. This local variablexis different from the global variablexdefined outside the function. - The
print(x)statement inside the functionfun()will print the value of the local variablex, which is 15. - After the function call, the global variable
xstill remains 12, but it is not printed.
So, the correct answer is 15.
Similar Questions
Choose the correct answerWhat will be the output of the following program?def fun(): try: print(1) finally: print(2)fun()Select all the correct statements given below.Options211221
What will be the output of the following Python code?def func(a, b=5, c=10): print('a is', a, 'and b is', b, 'and c is', c) func(3, 7)func(25, c = 24)func(c = 50, a = 100)1 pointOption 1Option 2Option 3None
Select the correct answerWhat will be the output of the following Python code?def fun(n): if (n > 100): return n - 5 return fun(fun(n+11)); print(fun(45))Options7410050Infinite loop
def func(a): a = a + 10 return a#Driver's codea = 5func(a)print(a)Options: Pick one correct answer from below105ErrorNone of the Above
What will be the output of the following Python code?def ct(): global y y+=3 print(y)y=8print("y")OptionsError11yy11
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.