Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

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 - Question

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.




3 - Question

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 - Question

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

c)

    bitter
    error

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 - Question

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 - Question

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 - Question

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 - Question

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.

Get weekly updates about new MCQs and other posts by joining 18000+ community of active learners