Q: Which of the following data structures is returned by the functions globals() and locals()?
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
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.
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")
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?
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
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.
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?
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.
Solution: There are many other problems can also be solved using recursion.
You Have Score    | /9 |