Test Series - python

Test Number 70/108

Q: Which of the following data structures is returned by the functions globals() and locals()?
A. list
B. set
C. dictionary
D. tuple
Solution: Both the functions, that is, globals() and locals() return value of the data structure dictionary.
Q: What will be the output of the following Python code?

x=1
def cg():
	global x
	x=x+1	
cg()
x
A. Error
B. 2
C. 1
D. 0
Solution: Since ‘x’ has been declared a global variable, it can be modified very easily within the function. Hence the output is 2.
Q: On assigning a value to a variable inside a function, it automatically becomes a global variable.
A. True
B. False
C. none
D. ..
Solution: On assigning a value to a variable inside a function, t automatically becomes a local variable. Hence the above statement is false.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: What will be the output of the following Python code?

def f(): x=4
x=1
f()
x
A. Error
B. Junk value
C. 4
D. 1
Solution: 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.
Q:  ______________ 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()
Solution: The function globals() returns a dictionary of the module namespace, whereas the function locals() returns a dictionary of the current namespace.
Q: Which is the most appropriate definition for recursion?
A. A function that calls itself
B. A function execution instance that calls another execution instance of the same function
C. A class method that calls another class method
D. An in-built method that is automatically called
Solution: The appropriate definition for a recursive function is a function execution instance that calls another execution instance of the same function either directly or indirectly.
Q: Only problems that are recursively defined can be solved using recursion.
A. True
B. False
C. none
D. ..
Solution: There are many other problems can also be solved using recursion.

You Have Score    /9