Welcome to our Python Functions quiz! Functions are essential building blocks in Python programming, allowing you to organize code, promote reusability, and enhance readability. This quiz is designed to evaluate your proficiency in defining, calling, and utilizing functions effectively. Get ready to sharpen your skills and deepen your understanding of Python function concepts!
30+ MCQs on Python Functions
Q1. What is the primary purpose of a function in Python?
a) To execute a block of code repeatedly
b) To organize code into manageable units and promote code reuse
c) To perform mathematical operations
d) To define conditional statements
Answer: b
Explanation: Functions in Python are primarily used to organize code into manageable units, promote code reuse, and improve readability.
Q2. What keyword is used to define a function in Python?
a) def
b) function
c) define
d) func
Answer: a
Explanation: The def
keyword is used to define a function in Python.
Q3. What is the purpose of the return statement in a function?
a) To stop the execution of the function
b) To print a value to the console
c) To return a value to the caller
d) To define a recursive function
Answer: c
Explanation: The return statement is used to return a value from a function to the caller.
Q4. What will be the output of the following code snippet?
def add(a, b):
return a + b
result = add(3, 5)
print(result)
a) 8
b) 3 + 5
c) 35
d) None
Answer: a
Explanation: The function add
takes two arguments and returns their sum, which is then printed.
Q5. Which of the following statements about function arguments in Python is true?
a) All arguments must have default values
b) Functions cannot have more than one argument
c) Arguments are passed by value
d) Arguments can have default values
Answer: d
Explanation: In Python, function arguments can have default values, allowing them to be omitted when the function is called.
Q6. What will be the output of the following code snippet?
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
a) Hello, Alice!
b) Hello, name!
c) Alice
d) None
Answer: a
Explanation: The function greet
takes a name argument and prints a greeting message with the provided name.
Q7. What is the purpose of the **kwargs parameter in a Python function definition?
a) To accept a variable number of positional arguments
b) To accept keyword arguments as a dictionary
c) To specify default values for keyword arguments
d) To raise an exception
Answer: b
Explanation: The **kwargs parameter allows a function to accept keyword arguments as a dictionary.
Q8. What will be the output of the following code snippet?
def multiply(*args):
result = 1
for num in args:
result *= num
return result
print(multiply(2, 3, 4))
a) 24
b) 9
c) 10
d) None
Answer: a
Explanation: The function multiply
takes a variable number of arguments and returns their product.
Q9. What is the purpose of the global keyword in Python?
a) To define a variable inside a function
b) To access a variable outside a function
c) To modify a variable defined in the global scope from within a function
d) To specify a variable as constant
Answer: c
Explanation: The global keyword is used to modify a variable defined in the global scope from within a function.
Q10. What will be the output of the following code snippet?
x = 5
def modify():
global x
x = 10
modify()
print(x)
a) 5
b) 10
c) Error
d) None
Answer: b
Explanation: The function modify
modifies the global variable x, changing its value to 10.
Q11. What is the purpose of the lambda keyword in Python?
a) To define anonymous functions
b) To define a variable
c) To import modules
d) To handle exceptions
Answer: a
Explanation: The lambda keyword is used to create anonymous functions, which are small, one-line functions without a name.
Q12. What will be the output of the following code snippet?
square = lambda x: x ** 2
print(square(5))
a) 10
b) 25
c) 5
d) None
Answer: b
Explanation: The lambda function square
squares its input, so square(5)
returns 25.
Q13. What is a recursive function?
a) A function that calls itself
b) A function with multiple return statements
c) A function that returns a dictionary
d) A function that takes multiple arguments
Answer: a
Explanation: A recursive function is a function that calls itself either directly or indirectly.
Q14. What will be the output of the following code snippet?
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
a) 5
b) 10
c) 120
d) None
Answer: c
Explanation: The recursive function factorial
computes the factorial of a number, so factorial(5)
returns 120.
Q15. Which of the following statements about recursive functions in Python is true?
a) Recursive functions cannot have a base case
b) Recursive functions always result in infinite loops
c) Recursive functions are less memory-efficient than iterative solutions
d) Recursive functions can be more readable and elegant for certain problems
Answer: d
Explanation: Recursive functions can be more readable and elegant for certain problems, such as those involving tree structures or mathematical recursion.
Q16. What will be the output of the following code snippet?
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(6))
a) 6
b) 8
c) 13
d) 21
Answer: c
Explanation: The recursive function fibonacci
computes the Fibonacci sequence, so fibonacci(6)
returns 13.
Q17. What is the purpose of the docstring in a Python function?
a) To define the function’s name
b) To provide documentation and information about the function
c) To specify the function’s return type
d) To specify the function’s parameters
Answer: b
Explanation: The docstring is used to provide documentation and information about the function, including its purpose, parameters, and return values.
Q18. What will be the output of the following code snippet?
def add(a, b):
"""This function adds two numbers."""
return a + b
print(add.__doc__)
a) add
b) This function adds two numbers.
c) a + b
d) None
Answer: b
Explanation: The print(add.__doc__)
statement prints the docstring associated with the add
function.
Q19. What is a decorator in Python?
a) A special type of function that modifies other functions
b) A keyword used to define a function
c) An object-oriented programming concept
d) A way to import modules
Answer: a
Explanation: A decorator is a special type of function that modifies other functions, typically by adding additional functionality.
Q20. What will be the output of the following code snippet?
def decorator(func):
def wrapper():
print("Before function execution")
func()
print("After function execution")
return wrapper
@decorator
def greet():
print("Hello!")
greet()
a) Before function execution
Hello!
After function execution
b) Before function execution
After function execution
Hello!
c) Hello!
Before function execution
After function execution
d) Hello!
Answer: a
Explanation: The decorator
function adds additional functionality before and after the execution of the greet
function.
Q21. What is the purpose of the nonlocal keyword in Python?
a) To define a variable inside a function
b) To access a variable outside a function
c) To modify a variable defined in the global scope from within a function
d) To modify a variable defined in an outer function’s scope from within a nested function
Answer: d
Explanation: The nonlocal keyword is used to modify a variable defined in an outer function’s scope from within a nested function.
Q22. What will be the output of the following code snippet?
def countdown(n):
if n <= 0:
print("Done!")
else:
print(n)
countdown(n - 1)
countdown(3)
a) 3 2 1 Done!
b) 1 2 3 Done!
c) Done! 1 2 3
d) Done!
Answer: a
Explanation: The countdown
function recursively prints numbers from n down to 1, then prints “Done!” when n reaches 0.
Q23. What is a recursive function’s base case?
a) The function call that starts the recursion
b) The function call that ends the recursion
c) The maximum number of recursive calls allowed
d) The function’s return value
Answer: b
Explanation: The base case of a recursive function is the condition that ends the recursion and prevents infinite recursion.
Q24. What will be the output of the following code snippet?
def squares(n):
for i in range(n):
yield i ** 2
for num in squares(3):
print(num)
a) 0 1 2
b) 1 4 9
c) 0 2 4
d) 0 1 4
Answer: d
Explanation: The squares
generator yields the square of each number up to n, so the output is 0, 1, and 4.
Q25. What is a generator in Python?
a) A function that generates random numbers
b) A function that returns a sequence of values lazily
c) A function that takes another function as an argument
d) A function that raises an exception
Answer: b
Explanation: A generator in Python is a function that returns a sequence of values lazily, allowing for efficient memory usage.
Q26. What will be the output of the following code snippet?
x = 10
def modify_global():
global x
x += 5
modify_global()
print(x)
a) 10
b) 15
c) 5
d) 20
Answer: b
Explanation: The modify_global
function modifies the global variable x, adding 5 to its value.
Q27. Which of the following is true about variable scope in Python?
a) Local variables can be accessed outside the function in which they are defined
b) Global variables take precedence over local variables
c) Variables defined inside a function have global scope
d) Variables defined inside a function have local scope
Answer: d
Explanation: Variables defined inside a function have local scope and can only be accessed within that function.
Q28. What is a closure in Python?
a) A function defined inside another function
b) A way to modify global variables from within a function
c) A function that returns another function
d) A way to handle exceptions
Answer: a
Explanation: A closure in Python refers to a function defined inside another function that retains the scope of the enclosing function.
Q29. What will be the output of the following code snippet?
def power(base, exponent=2):
return base ** exponent
result1 = power(2)
result2 = power(2, 3)
print(result1, result2)
a) 4 8
b) 4 6
c) 8 4
d) 6 8
Answer: a
Explanation: The function power
raises the base to the exponent, with the default exponent being 2.
Q30. What will be the output of the following code snippet?
def square(x):
return x ** 2
numbers = [1, 2, 3, 4]
squared_numbers = map(square, numbers)
print(list(squared_numbers))
a) [1, 4, 9, 16]
b) [1, 2, 3, 4]
c) [2, 4, 6, 8]
d) [1, 3, 5, 7]
Answer: a
Explanation: The map
function applies the square
function to each element in the numbers
list.
Q31. What will be the output of the following code snippet?
def add(a, b):
return a + b
def subtract(a, b):
return a - b
operations = {'add': add, 'subtract': subtract}
result1 = operations['add'](5, 3)
result2 = operations['subtract'](7, 2)
print(result1, result2)
a) 8 5
b) 2 5
c) 5 2
d) 8 7
Answer: a
Explanation: The dictionary operations
maps operation names to their corresponding functions, which are then invoked with arguments.
Q32. What will be the output of the following code snippet?
def multiply(*args):
result = 1
for num in args:
result *= num
return result
print(multiply(2, 3, 4))
a) 24
b) 9
c) 10
d) None
Answer: a
Explanation: The function multiply
takes a variable number of arguments and returns their product.
Q33. What will be the output of the following code snippet?
def is_even(n):
return n % 2 == 0
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(is_even, numbers)
print(list(even_numbers))
a) [1, 3, 5]
b) [2, 4, 6]
c) [1, 2, 3, 4, 5, 6]
d) []
Answer: b
Explanation: The filter
function applies the is_even
function to each element in the numbers
list and returns those for which the function returns True.
Congratulations on completing the Python Functions quiz! Functions are the cornerstone of Python programming, enabling you to encapsulate logic, promote reusability, and enhance code organization. By mastering function definition, parameter handling, return values, and scope management, you’re well-equipped to write clean, modular, and efficient Python code. Keep honing your skills and exploring advanced function concepts to become a proficient Python programmer. Well done, and keep coding!
You can also enroll in out free Python Course Today!
Read our more articles related to MCQs in Python: