Python MCQ on Functions

Functions are crucial in any programming language, enabling modularity and code reuse. They allow you to define a set of instructions that you can use repeatedly throughout your code. Let’s test your understanding of Python functions with these 12+ Multiple Choice Questions.

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

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

Answer:

d) def

Explanation:

In Python, the def keyword is used to declare or define a function.

2. What will be the output of the following code?

def greet():
    print("Hello!")
print(greet())
a) Hello!
b) None
c) Hello! None
d) Error

Answer:

c) Hello! None

Explanation:

The function greet() prints “Hello!” but does not return any value. By default, Python functions return None if no return value is specified.

3. Which of the following is a correct function header?

a) def function():
b) function def():
c) def function(): {}
d) function():

Answer:

a) def function():

Explanation:

Functions in Python are defined using the def keyword, followed by the function name and parentheses.

4. What is the purpose of the return statement in functions?

a) To return to the start of the function
b) To exit the function and return a value
c) To print the output
d) To loop through the function

Answer:

b) To exit the function and return a value

Explanation:

The return statement is used to exit a function and return a value.

5. Which of the following functions takes two arguments and returns their sum?

a) def sum(a, b): print(a + b)
b) def sum(a + b): return a + b
c) def sum(a, b): return a + b
d) sum(a, b): return a + b

Answer:

c) def sum(a, b): return a + b

Explanation:

The correct way to define a function that takes two arguments and returns their sum is option c.

6. How do you call a function named my_function?

a) call my_function()
b) my_function()
c) my_function(call)
d) def my_function()

Answer:

b) my_function()

Explanation:

Functions are called by their name followed by parentheses.

7. Which of the following is the correct way to define a function that takes any number of positional arguments?

a) def function(*args):
b) def function(args*):
c) def function(args…):
d) def function(…args):

Answer:

a) def function(*args):

Explanation:

The *args syntax in a function header allows the function to accept any number of positional arguments.

8. What is a lambda function in Python?

a) A regular named function
b) A small anonymous function
c) A special method
d) A type of loop

Answer:

b) A small anonymous function

Explanation:

A lambda function is a small, unnamed (anonymous) function defined using the lambda keyword.

9. Which of the following is a correct lambda function that squares a number?

a) lambda x: x*x
b) x -> x*x
c) def lambda x: x*x
d) lambda x -> x*x

Answer:

a) lambda x: x*x

Explanation:

The correct syntax for a lambda function to square a number in Python is lambda x: x*x.

10. Which of the following functions is a recursive function?

a) A function that calls itself
b) A function that is called by another function
c) A function that loops through a list
d) A function that contains a loop

Answer:

a) A function that calls itself

Explanation:

A recursive function is a function that calls itself in order to solve a problem.

11. What is the purpose of the global keyword in a function?

a) To define a global variable inside the function
b) To use a global variable inside the function
c) To create a global function
d) To exit the function and return to the global scope

Answer:

b) To use a global variable inside the function

Explanation:

The global keyword is used inside a function to refer to a variable that is defined in the global scope.

12. What is the output of the following code?

def add(x, y):
    return x + y

def operate(func, x, y):
    return func(x, y)

print(operate(add, 3, 4))

a) 12
b) 7
c) None
d) Error

Answer:

b) 7

Explanation:

The operate function takes another function as an argument and calls it with x and y. In this case, it calls the add function, which returns 3 + 4, i.e., 7.

13. Which of the following can be used as default values for function arguments?

a) Numbers
b) Strings
c) Lists
d) All of the above

Answer:

d) All of the above

Explanation:

In Python, numbers, strings, lists, and many other types can be used as default values for function arguments.


I hope you found this MCQ series insightful! Testing your knowledge on functions can help solidify your understanding and make you more proficient in Python. Keep practicing and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top