Python Functions MCQ Questions and Answers

Functions in Python are blocks of code that are defined with the def keyword. They are executed only when they are called. Functions can accept input data known as parameters or arguments, and they can also return data as a result. This allows you to encapsulate and reuse code, making your programs more organized and efficient. Here we present 15 multiple-choice questions to test your knowledge of Python Functions. Each MCQ has the correct answer with an explanation. 

1. How is a function defined in Python?

a) function myFunction():
b) def myFunction():
c) create myFunction():
d) make myFunction():

Answer:

b) def myFunction()

Explanation:

In Python, a function is defined using the 'def' keyword followed by the function name and parentheses.

2. How do you call a function named 'myFunction' in Python?

a) call myFunction()
b) myFunction()
c) execute myFunction()
d) run myFunction()

Answer:

b) myFunction()

Explanation:

A function is called by writing its name followed by parentheses.

3. What is the correct way to define a function with one parameter 'x' in Python?

a) def myFunction(x):
b) def myFunction parameter x:
c) def myFunction with x:
d) def myFunction: x

Answer:

a) def myFunction(x):

Explanation:

Function parameters are specified within the parentheses in the function definition.

4. How do you return a value from a function in Python?

a) return value
b) output value
c) result value
d) give value

Answer:

a) return value

Explanation:

The 'return' statement is used to return a value from a function.

5. What is a default parameter in Python functions?

a) A parameter that is optional
b) A parameter that must be provided
c) A parameter that has a default value
d) The first parameter in a function

Answer:

c) A parameter that has a default value

Explanation:

Default parameters are those that have a default value and can be omitted when calling the function.

6. How do you create a function with variable number of arguments in Python?

a) def myFunction(*args):
b) def myFunction(...):
c) def myFunction(args[]):
d) def myFunction(varargs):

Answer:

a) def myFunction(*args):

Explanation:

The *args syntax is used to pass a variable number of arguments to a function.

7. What does a lambda function in Python do?

a) Iterates over a sequence
b) Creates a new data type
c) Defines a small anonymous function
d) Initializes a global variable

Answer:

c) Defines a small anonymous function

Explanation:

Lambda functions are small anonymous functions defined using the lambda keyword.

8. How do you specify a docstring in a Python function?

a) # This is a docstring
b) /* This is a docstring */
c) <!-- This is a docstring -->
d) """This is a docstring"""

Answer:

d) """This is a docstring"""

Explanation:

Docstrings are specified using triple quotes at the beginning of a function.

9. What is the output of the following code?

a) None
b) 0
c) ""
d) Error

Answer:

a) None

Explanation:

The pass statement in a function does nothing, and a function without a return statement returns None by default.

10. How do you define a function that takes an unlimited number of keyword arguments?

a) def myFunction(**kwargs):
b) def myFunction(*kwargs):
c) def myFunction(kwargs**):
d) def myFunction(**args):

Answer:

a) def myFunction(**kwargs):

Explanation:

**kwargs allows passing a variable number of keyword arguments to a function.

11. What is recursion in Python?

a) A function calling another function
b) A function that never ends
c) A function calling itself
d) A function iterating over a loop

Answer:

c) A function calling itself

Explanation:

Recursion occurs when a function calls itself.

12. How do you make a variable defined inside a function accessible outside the function?

a) Use the global keyword
b) Define the variable as static
c) Return the variable
d) Declare the variable outside the function

Answer:

a) Use the global keyword

Explanation:

The global keyword allows a variable defined inside a function to be accessible globally.

13. What is an anonymous function in Python?

a) A function defined without a name
b) A function defined inside another function
c) A function with no parameters
d) A function that returns None

Answer:

a) A function defined without a name

Explanation:

Anonymous functions, also known as lambda functions, are defined without a name.

14. What is a function decorator in Python?

a) A tool to add functionality to an existing function
b) A comment that describes a function
c) A function that deletes other functions
d) A syntax for defining multiple functions at once

Answer:

a) A tool to add functionality to an existing function

Explanation:

Decorators are used in Python to modify or extend the behavior of functions or methods.

15. What does the 'yield' keyword do in Python?

a) Pauses the function and saves its state
b) Stops the function permanently
c) Returns a value and exits the function
d) Skips the iteration in a loop

Answer:

a) Pauses the function and saves its state

Explanation:

The 'yield' keyword is used in generator functions to pause the function execution and send a value back to the caller, but retains enough state to enable the function to resume where it left off.


Comments