Python Questions and Answers – Global vs Local Variables – 2

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Global vs Local Variables – 2”.

1. Which of the following data structures is returned by the functions globals() and locals()?
a) list
b) set
c) dictionary
d) tuple
View Answer

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

2. 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
View Answer

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

3. On assigning a value to a variable inside a function, it automatically becomes a global variable.
a) True
b) False
View Answer

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

4. 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
advertisement

c)

    bitter
    error
advertisement

d) bitterbutter
View Answer

Answer: c
Explanation: 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.

5. 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
View Answer

Answer: d
Explanation: 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.

6. 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
View Answer

Answer: b
Explanation: 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.

7. 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
View Answer

Answer: d
Explanation: 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.

8. ______________ 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()
View Answer
Answer: c
Explanation: The function globals() returns a dictionary of the module namespace, whereas the function locals() returns a dictionary of the current namespace.
 
 

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.