Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

Consider the code given below:

Question 12 CBSE Sample Question Paper 2024 Boards - Teachoo.PNG

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

Options:

a. global a

b. global b=100

c. global b

d. global a=100

Answer

Answer by student:

c. global b

Detailed answer by teachoo:

To answer this question, we need to know the syntax and usage of global variables and local variables in Python.

  • A global variable is a variable that is defined outside a function and can be accessed by any function in the program. For example, b = 100 is a global variable that is defined at the beginning of the program and can be used by any function.
  • A local variable is a variable that is defined inside a function and can only be accessed by that function. For example, a is a local variable that is defined as a parameter of the test function and can only be used by the test function.
  • If we want to modify a global variable inside a function, we need to use the global keyword before the variable name. This tells Python that we are referring to the global variable, not a local variable with the same name. For example, global b inside the test function will allow us to change the value of b globally.

Let’s go through each of the options:

  • a. global a: This is incorrect because it will make ‘a’ a global variable, not a local variable. This will cause an error because a is not defined globally before the test function is called. Also, this will not change the value of b globally, which is required for the output to be 110.
  • b. global b=100: This is incorrect because it will assign 100 to b again, which will overwrite the previous value of b. This will make b always equal to 100 + a, regardless of how many times the test function is called. Also, this is not a valid syntax for using the global keyword. The global keyword should be followed by only the variable name, not an assignment statement.
  • c. global b: This is correct because it will make b a global variable inside the test function. This will allow us to change the value of ‘b’ globally by adding ‘a’ to it. This will produce the output 110 when test(10) is called, because ‘b’ will become 100 + 10 = 110.
  • d. global a=100: This is incorrect because it will assign 100 to a, which will overwrite the value of a passed as an argument to the test function. This will make ‘a’ always equal to 100, regardless of what value is given to the test function. Also, this is not a valid syntax for using the global keyword. The global keyword should be followed by only the variable name, not an assignment statement.

So, the correct answer is c. global b.

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.