Python Function MCQ : Set 1

Python Function MCQ

1). Which keyword is used to define a function in Python?

a) def
b) func
c) define
d) function

Correct answer is: a) def
Explanation: The keyword “def” is used to define a function in Python.

2). In a function what is the purpose of a return statement?

a) It defines the function name.
b) It calls another function.
c) It specifies the input parameters.
d) It returns a value from the function.

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

3). Which of the following is a valid way to call a function in Python?

a) function_name()
b) functionName()
c) FunctionName()
d) All of the above.

Correct answer is: d) All of the above.
Explanation: All of the given options are valid ways to call a function in Python.

4). What is the purpose of the “pass” statement in Python?

a) It is used to define an empty function.
b) It is used to skip the execution of a function.
c) It is used to indicate the end of a function.
d) It is used to define a placeholder in a function.

Correct answer is: d) It is used to define a placeholder in a function.
Explanation: The “pass” statement is used as a placeholder when you want to define a function without any implementation.

5). Which scope in Python can access variables defined inside a function?

a) Global scope
b) Local scope
c) Built-in scope
d) None of the above

Correct answer is: a) Global scope
Explanation: Variables defined inside a function have local scope and can only be accessed within that function, unless they are explicitly marked as global.

6). What happens if a function is called with fewer arguments than specified in the function definition?

a) An error is raised.
b) The function will run without any issues.
c) The missing arguments will be assigned default values.
d) The function will return None.

Correct answer is: a) An error is raised.
Explanation: If a function is called with fewer arguments than specified in the function definition, a TypeError will be raised.

7). What is the purpose of the “lambda” keyword in Python?

a) It defines a lambda expression.
b) It is used to create a new function.
c) It is used to define the main function.
d) It is used to import external modules.

Correct answer is: a) It defines a lambda expression.
Explanation: The “lambda” keyword is used to define anonymous functions, also known as lambda expressions.

8). Which of the following is a valid way to pass arguments to a function?

a) Positional arguments
b) Keyword arguments
c) Default arguments
d) All of the above

Correct answer is: d) All of the above
Explanation: All the options (positional, keyword, and default arguments) are valid ways to pass arguments to a function.

9). What is the maximum number of return statements allowed in a function?

a) 1
b) 2
c) 3
d) There is no limit.

Correct answer is: d) There is no limit.
Explanation: There is no limit to the number of return statements in a function. However, only one return statement is executed during the function call.

10). What is the purpose of the “map” function in Python?

a) It applies a function to every item in an iterable.
b) It returns the maximum value from an iterable.
c) It converts an iterable to a list.
d) It sorts the items in an iterable.

Correct answer is: a) It applies a function to every item in an iterable.
Explanation: The “map” function is used to apply a given function to each item of an iterable and returns an iterator with the results.

11). Which of the following is a recursive function?

a) A function that calls itself.
b) A function that uses a loop.
c) A function that imports another module.
d) A function that returns multiple values.

Correct answer is: a) A function that calls itself.
Explanation: It is a function that calls itself either directly or indirectly.

12). Which built-in function is used to find the length of an object?

a) count()
b) length()
c) size()
d) len()

Correct answer is: d) len()
Explanation: The `len()` function is used to find the length of an object, such as a string, list, or tuple.

13). What is the purpose of the “global” keyword in Python?

a) It defines a global variable.
b) It imports a module.
c) It creates a new function.
d) It raises an error.

Correct answer is: a) It defines a global variable.
Explanation: The “global” keyword is used to indicate that a variable is defined in the global scope, allowing it to be accessed from any part of the program.

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

a) def my_function():
b) define my_function():
c) my_function = def():
d) lambda x: x + 1

Correct answer is: c) my_function = def():
Explanation: The option c) is not a valid way to define a function in Python. The correct syntax is `def function_name():`.

15). Which of the following is NOT a valid function call in Python?

a) my_function(1, 2)
b) my_function(x=1, y=2)
c) my_function(1, y=2)
d) my_function(x=1, 2)

Correct answer is: d) my_function(x=1, 2)
Explanation: In a function call, keyword arguments must follow positional arguments. In option d), a positional argument is followed by a keyword argument, which is not valid.

16). What is the purpose of the “__init__” method in a Python class?

a) It defines the class name.
b) It initializes the object’s state.
c) It imports external modules.
d) It defines class methods.

Correct answer is: b) It initializes the object’s state.
Explanation: The method “__init__” is a method in Python classes that is called when an object is created. It initializes the object’s state.

17). Which of the following is a built-in function in Python for sorting?

a) sort()
b) sorted()
c) order()
d) arrange()

Correct answer is: b) sorted()
Explanation: The `sorted()` function is a built-in function in Python that is used to sort iterables, such as lists or tuples.

18). What is the purpose of the “isinstance()” function in Python?

a) It checks if two objects are equal.
b) It checks if an object is an instance of a class.
c) It converts an object to a different type.
d) It finds the index of an element in a list.

Correct answer is: b) It checks if an object is an instance of a class.
Explanation: The function `isinstance()` is used to check if an object is an instance of a specified class or its subclasses.

19). 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.

20). What is the purpose of the “enumerate()” function in Python?

a) It counts the number of occurrences of an element in an iterable.
b) It returns a reversed version of an iterable.
c) It assigns indices to items in an iterable.
d) It removes duplicates from an iterable.

Correct answer is: c) It assigns indices to items in an iterable.
Explanation: The “enumerate()” function is used to assign indices to items in an iterable, creating tuples of the form (index, item).

Leave a Comment