CBSE Class 12 Computer Science Question 88 of 103

Working with Functions — Question 21

Back to all questions
21
Question

Question 15

Find and write the output of the following python code :

a = 10
def call(): 
    global a
    a = 15
    b = 20
    print(a) 
call()
Answer
Output

15

Explanation
  1. a = 10 — This line assigns the value 10 to the global variable a.
  2. def call() — This line defines a function named call.
  3. a = 15 — Inside the call function, this line assigns the value 15 to the global variable a. As global keyword is used earlier, this assignment modifies the value of the global variable a.
  4. b = 20 — Inside the call function, this line assigns the value 20 to a local variable b.
  5. print(a) — This line prints the value of the global variable a, which is 15. This is because we've modified the global variable a inside the call function.