Computer Science
What will be the output of the following Python code ?
V = 25
def Fun(Ch):
V = 50
print(V, end = Ch)
V *= 2
print(V, end = Ch)
print(V, end = "*")
Fun("!")
print(V)
- 25*50!100!25
- 50*100!100!100
- 25*50!100!100
- Error
Answer
25*50!100!25
Working
V = 25
— initializes global variable V to 25.print(V, end = "*")
— Prints the global variableV
(25) followed by an asterisk without a newline.Fun("!")
— Calls the functionFun
with the argument "!".- Inside function
Fun
,V = 50
initializes a local variableV
within the function with the value 50. This variable is different from the global variableV
that has the value 25. print(V, end = Ch)
— Prints the local variableV
(50) followed by the character passed as an argument to the function, which is "!".V *= 2
— Multiplies the local variableV
(50) by 2, making it 100.print(V, end = Ch)
— Prints the updated local variable V (100) followed by the character "!".- The function
Fun
returns and the lineprint(V)
is executed. It prints the global variableV
without any newline character. The global variableV
remains unchanged (25).
Related Questions
What is the output of following code fragments ?
def increment(n): n.append([4]) return n L = [1, 2, 3] M = increment(L) print(L, M)
What is the output of following code fragments ?
def increment(n): n.append([49]) return n[0], n[1], n[2], n[3] L = [23, 35, 47] m1, m2, m3, m4 = increment(L) print(L) print(m1, m2, m3, m4) print(L[3] == m4)
Write a function that takes amount-in-dollars and dollar-to-rupee conversion price; it then returns the amount converted to rupees. Create the function in both void and non-void forms.
Write a function to calculate volume of a box with appropriate default values for its parameters. Your function should have the following input parameters :
(a) length of box ;
(b) width of box ;
(c) height of box.
Test it by writing complete program to invoke it.