CBSE Class 12 Computer Science Question 12 of 42

Solved 2024 Sample Question Paper CBSE Class 12 Computer Science (083) — Question 12

Back to all questions
12
Question

Question 12

Consider the code given below:

b = 100
def test(a):
............... #missing statement
    b = b + a
    print(a, b)
test(10)
print(b)

Which of the following statements should be given in the blank for #Missing Statement, if the output produced is 110?

  1. global a
  2. global b = 100
  3. global b
  4. global a = 100
Answer

global b

Reason — When we want to modify a global variable within a function in Python, we need to use the global keyword followed by the name of the global variable. In this case, since the variable b is declared as a global variable outside the function, we need to declare it as a global variable again inside the function using global b before attempting to modify its value.