Computer Science

What will be the output of the following code ?

c = 10
def add():
    global c
    c = c + 2
    print(c, end = '#')
add()
c = 15
print(c, end = '%')
  1. 12%15#
  2. 15#12%
  3. 12#15%
  4. 12%15#

Python Functions

5 Likes

Answer

12#15%

Reason

  1. The variable c is initially set to 10.
  2. The function add() is defined. Inside this function, the global keyword is used to indicate that the variable c refers to the global variable defined outside the function.
  3. Inside add(): When add() is called:
    • The value of c (which is 10) is accessed.
    • It is then incremented by 2, making c equal to 12.
    • The new value of c (12) is printed, followed by a # symbol.
  4. After calling the function, the global c is now 12. However, it is then explicitly set to 15.
  5. Finally, the new value of c (15) is printed, followed by a % symbol.

Answered By

1 Like


Related Questions