KnowledgeBoat Logo

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) 
  1. 25*50!100!25
  2. 50*100!100!100
  3. 25*50!100!100
  4. Error

Python

Python Functions

10 Likes

Answer

25*50!100!25

Working

  1. V = 25 — initializes global variable V to 25.
  2. print(V, end = "*") — Prints the global variable V (25) followed by an asterisk without a newline.
  3. Fun("!") — Calls the function Fun with the argument "!".
  4. Inside function Fun, V = 50 initializes a local variable V within the function with the value 50. This variable is different from the global variable V that has the value 25.
  5. print(V, end = Ch) — Prints the local variable V (50) followed by the character passed as an argument to the function, which is "!".
  6. V *= 2 — Multiplies the local variable V (50) by 2, making it 100.
  7. print(V, end = Ch) — Prints the updated local variable V (100) followed by the character "!".
  8. The function Fun returns and the line print(V) is executed. It prints the global variable V without any newline character. The global variable V remains unchanged (25).

Answered By

2 Likes


Related Questions