Python MCQ’s on Global and Local Variables: SET 2

11. What will be the output of the following Python code?

x = 5

def f1():

global x

x = 4

def f2(a,b):

global x

return a+b+x

f1()

total = f2(1,2)

print(total)

a) Error

b) 7

c) 8

d) 15

Check Answer

Answer: b
In the code shown above, the variable ‘x’ has been declared as a global variable under both the functions f1 and f2. The value returned is a+b+x = 1+2+4 = 7.

12. What will be the output of the following Python code?

x=100

def f1():

global x

x=90

def f2():

global x

x=80

print(x)

a) 100

b) 90

c) 80

d) Error

Check Answer

Answer: a
The output of the code shown above is 100. This is because the variable ‘x’ has been declared as global within the functions f1 and f2.

13. Read the following Python code carefully and point out the global variables?

y, z = 1, 2

def f():

global x

x = y+z

a) x

b) y and z

c) x, y and z

d) Neither x, nor y, nor z

Check Answer

Answer: c
In the code shown above, x, y and z are global variables inside the function f. y and z are global because they are not assigned in the function. x is a global variable because it is explicitly specified so in the code. Hence, x, y and z are global variables.

14. Which of the following data structures is returned by the functions globals() and locals()?

a) list

b) set

c) dictionary

d) tuple

Check Answer

Answer: c
Both the functions, that is, globals() and locals() return value of the data structure dictionary.

15. What will be the output of the following Python code?

x=1

def cg():

global x

x=x+1

cg()

x

a) 2

b) 1

c) 0

d) Error

Check Answer

Answer: a
Since ‘x’ has been declared a global variable, it can be modified very easily within the function. Hence the output is 2.

16. On assigning a value to a variable inside a function, it automatically becomes a global variable.

a) True

b) False

Check Answer

Answer: b
On assigning a value to a variable inside a function, t automatically becomes a local variable. Hence the above statement is false.

17. What will be the output of the following Python code?

e=”butter”

def f(a): print(a)+e

f(“bitter”)

a) error

b)

butter
error

c)

bitter
error

d) bitterbutter

Check Answer

Answer: c
The output of the code shown above will be ‘bitter’, followed by an error. The error is because the operand ‘+’ is unsupported on the types used above.

18. What happens if a local variable exists with the same name as the global variable you want to access?

a) Error

b) The local variable is shadowed

c) Undefined behavior

d) The global variable is shadowed

Check Answer

Answer: d
If a local variable exists with the same name as the local variable that you want to access, then the global variable is shadowed. That is, preference is given to the local variable.

19. What will be the output of the following Python code?

a=10

globals()[‘a’]=25

print(a)

a) 10

b) 25

c) Junk value

d) Error

Check Answer

Answer: b
In the code shown above, the value of ‘a’ can be changed by using globals() function. The dictionary returned is accessed using key of the variable ‘a’ and modified to 25.

20. What will be the output of the following Python code?

def f(): x=4

x=1

f()

x

a) Error

b) 4

c) Junk value

d) 1

Check Answer

Answer: d
In the code shown above, when we call the function f, a new namespace is created. The assignment x=4 is performed in the local namespace and does not affect the global namespace. Hence the output is 1.

21. ______________ returns a dictionary of the module namespace.

________________ returns a dictionary of the current namespace.

a)

locals()
globals()

b)

locals()
locals()

c)

globals()
locals()

d)

globals()
globals()

Check Answer

Answer: c
The function globals() returns a dictionary of the module namespace, whereas the function locals() returns a dictionary of the current namespace.

Page  1  2

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

Leave a Comment