Python Function MCQ : Set 2

Python Function MCQ

1). What is the purpose of the “return” statement in Python?

a) It defines a loop.
b) It handles exceptions.
c) It terminates the execution of a loop.
d) It specifies the value to be returned by a function.

Correct answer is: d) It specifies the value to be returned by a function.
Explanation: The “return” statement is used to specify the value that a function should return when it is called.

2). Which of the following is a valid way to define a default argument in a function?

a) def my_func(x = 0):
b) def my_func(x, default=0):
c) def my_func(default=0, x):
d) def my_func(x = 0, default):

Correct answer is: a) def my_func(x = 0):
Explanation: Option a) is a valid way to define a default argument in a function. The default value is specified after the “=” sign.

3). What is the purpose of the “reduce()” function in Python?

a) It applies a function to every item in an iterable.
b) It removes duplicates from an iterable.
c) It filters out elements from an iterable based on a condition.
d) It performs a cumulative computation on an iterable.

Correct answer is: d) It performs a cumulative computation on an iterable.
Explanation: The “reduce()” function is used to perform a cumulative computation on an iterable by applying a specified function to the elements.

4). What is the function header?

a) The name of the function and the list of parameters that the function takes.
b) The list of parameters that the function takes.
c) The body of the function.
d) The return value of the function.

Correct answer is: a) The name of the function and the list of parameters that the function takes.

5). What is the body of a function?

a) The code that is executed when the function is called.
b) The list of parameters that the function takes.
c) The return value of the function.
d) The name of the function.

Correct answer is: a) The code that is executed when the function is called.

6). What is a parameter?

a) A variable that is defined inside a function.
b) A variable that is used to store the return value of a function.
c) A variable that is passed to a function.
d) A variable that is used to control the flow of execution of a function.

Correct answer is: c) A variable that is passed to a function.

7). What is the difference between a global variable and local variable?

a) A local variable is defined inside a function, while a global variable is defined outside of a function.
b) A local variable can only be accessed inside the function that defines it, while a global variable can be accessed anywhere in the program.
c) A local variable is created when the function is called, and it is destroyed when the function returns. A global variable is created when the program starts, and it exists until the program ends.
d) All of the above.

Correct answer is: d) All of the above.

8). What is the difference between a built-in function and a user-defined function?

a) A built-in function is a function that is provided by the Python language, while a user-defined function is a function that is defined by the user.
b) A built-in function cannot be modified, while a user-defined function can be modified.
c) A built-in function can only be called from within a function, while a user-defined function can be called from anywhere in the program.
d) All of the above.

Correct answer is: d) All of the above.

9). What is the advantage of using functions?

a) Functions make code more readable and maintainable.
b) Functions can be reused in different parts of the program.
c) Functions can help to improve performance.
d) All of the above.

Correct answer is: d) All of the above.

10). What is the disadvantage of using functions?

a) Functions can make code more complex.
b) Functions can make code less efficient.
c) Functions can make code more difficult to debug.
d) None of the above.

Correct answer is: d) None of the above.

11). What is the purpose of the “zip()” function in Python?

a) It combines two or more iterables element-wise.
b) It compresses data into a ZIP file.
c) It returns the size of an iterable.
d) It reverses the order of elements in an iterable.

Correct answer is: a) It combines two or more iterables element-wise.
Explanation: The “zip()” function is used to combine two or more iterables element-wise, creating an iterator of tuples.

12). Which of the following is a valid way to access the documentation string of a function in Python?

a) help(my_function)
b) my_function.__doc__
c) my_function.document
d) All of the above

Correct answer is: d) All of the above
Explanation: All the given options are valid ways to access the documentation string (docstring) of a function in Python.

13). Which of the following is a valid way to define a recursive function in Python?

a) def my_func():
b) def my_func(x):
c) def my_func():
d) def my_func(x = 0):

Correct answer is: b) def my_func(x):
Explanation: Option b) is a valid way to define a recursive function in Python. The function should have at least one parameter to enable recursive calls with different arguments.

14). Which of the following is a valid way to define a lambda function in Python?

a) lambda x: x + 1
b) def lambda(x): return x + 1
c) def my_func(x): return x + 1
d) lambda: return x + 1

Correct answer is: a) lambda x: x + 1
Explanation: Option a) is a valid way to define a lambda function in Python. It takes a parameter `x` and returns `x + 1`.

15). What is the purpose of the “filter()” function in Python?

a) It applies a function to every item in an iterable.
b) It removes duplicates from an iterable.
c) It filters out elements from an iterable based on a condition.
d) It counts the number of occurrences of an element in an iterable.

Correct answer is: c) It filters out elements from an iterable based on a condition.
Explanation: The “filter()” function is used to filter out elements from an iterable based on a specified condition.

16). What is the output of the following code?

def func(x):
    return x * 3

result = func(“abc”)
print(result)

a) a
b) abc
c) abcabcabc
d) None

Correct answer is: c) abcabcabc
Explanation: The function func multiplies the input string x by 3. In this case, “abc” multiplied by 3 results in “abcabcabc”.

17). What is the output of the following code?

def func(x):
    return x * 2

value = 4
result = func(value)
print(result)

a) 3
b) 8
c) 9
d) None

Correct answer is: b) 8
Explanation: The variable value is passed to the function func, and the function returns the value of x multiplied by 2. In this case, value is 4, so the function returns 4 * 2 = 8.

18). What is the output of the following code?

def func(x):
    if x == “”:
        return “Empty”
    else:
        return “Not empty”

result = func(“”)
print(result)

a) Empty
b) Not empty
c) Error
d) None

Correct answer is: a) Empty
Explanation: The function func checks if the input string x is empty. In this case, since x is an empty string, it returns “Empty”.

19). What is the output of the following code?

def func(x):
    return len(x)

result = func([1, 2, 3, 4])
print(result)

a) 1
b) 2
c) 3
d) 4

Correct answer is: d) 4
Explanation: The function func returns the length of the input list x using the “len()” function. In this case, the list has 4 elements, so the output is 4.

20). What is the output of the following code?

def func(x):
    return x[::-1]

result = func(“Python”)
print(result)

a) nohtyP
b) P
c) Python
d) None

Correct answer is: a) nohtyP
Explanation: The function func returns the reverse of the input string x using slicing with a step of -1. In this case, “Python” reversed becomes “nohtyP”.

Leave a Comment