Python Multiple Choice Questions & Answers (MCQs) focuses on “Function – 4”.
Q 1. What is a variable defined outside a function referred to as?
A. A static variable
B. A global variable
C. A local variable
D. An automatic variable
Show Answer
Answer:-B. A global variableExplanation
The value of a variable defined outside all function definitions is referred to as a global variable and can be used by multiple functions of the program.
Q 2. What is a variable defined inside a function referred to as?
A. A global variable
B. A volatile variable
C. A local variable
D. An automatic variable
Show Answer
Answer:-C. A local variableExplanation
The variable inside a function is called as local variable and the variable definition is confined only to that function.
Q 3. What will be the output of the following Python code?
i=0
def change(i):
i=i+1
return i
change(1)
print(i)
A. 0
B. 1
C. Nothing is displayed
D. An exception is thrown
Show Answer
Answer:-A. 0Explanation
Any change made in to an immutable data type in a function isn’t reflected outside the function.Q 4. What will be the output of the following Python code?
def a(b):
b = b + [5]
c = [1, 2, 3, 4]
a(c)
print(len(c))
A. 4
B. 5
C. 1
D. An exception is thrown
Show Answer
Answer:-A. 4Explanation
Since a list is mutable, any change made in the list in the function is reflected outside the function.Q 5. What will be the output of the following Python code?
a=10
b=20
def change():
global b
a=45
b=56
change()
print(a)
print(b)
A. 10
56
B. 45
56
C. 10
20
D. Syntax Error
Show Answer
Answer:-A. 10 56Explanation
The statement “global b” allows the global value of b to be accessed and changed. Whereas the variable a is local and hence the change isn’t reflected outside the function.
Q 6. What will be the output of the following Python code?
def change(i = 1, j = 2):
i = i + j
j = j + 1
print(i, j)
change(j = 1, i = 2)
A. An exception is thrown because of conflicting values
B. 1 2
C. 3 3
D. 3 2
Show Answer
Answer:-D. 3 2Explanation
The values given during function call is taken into consideration, that is, i=2 and j=1.
Q 7. What will be the output of the following Python code?
def change(one, *two):
print(type(two))
change(1,2,3,4)
A. Integer
B. Tuple
C. Dictionary
D. An exception is thrown
Show Answer
Answer:-B . TupleExplanation
The parameter two is a variable parameter and consists of (2,3,4). Hence the data type is tuple.
Q 8. If a function doesn’t have a return statement, which of the following does the function return?
A. int
B. null
C. An exception is thrown without the return statement
D. None
Show Answer
Answer:-D. NoneExplanation
A function can exist without a return statement and returns None if the function doesn’t have a return statement.
Q 9. What will be the output of the following Python code?
def display(b, n):
while n > 0:
print(b,end=””)
n=n-1
display(‘z’,3)
A. zzz
B. zz
C. An exception is executed
D. Infinite loop
Show Answer
Answer:-A. zzzExplanation
The loop runs three times and ‘z’ is printed each time.
Q 10. What will be the output of the following Python code?
def find(a, **b):
print(type(b))
find(‘letters’,A=’1′,B=’2′)
A. String
B. Tuple
C. Dictionary
D. An exception is thrown
Leave a Reply