Python Functions Quiz
Welcome to the Python Functions Quiz!
Functions are a fundamental part of Python that help make code reusable, readable, and modular. This quiz will test your knowledge of Python functions, including syntax, arguments, return values, scope, built-in functions, and more.
Table of Contents:
- Python quizzes for beginners series
Functions Quiz
Quiz Questions
1. What is the correct way to define a function in Python?
a) function my_func():
b) def my_func:
c) def my_func():
d) create function my_func()
Answer
c) def my_func():
2. What keyword is used to exit a function and return a value?
a) exit
b) stop
c) return
d) break
Answer
c) return
3. What will be the output of the following code?
def add(a, b):
return a + b
print(add(2, 3))
a) 2 + 3
b) 5
c) None
d) Error
Answer
b) 5
4. What will be the output of this code?
def my_func(x=5):
return x * 2
print(my_func())
a) 10
b) 5
c) None
d) Error
Answer
a) 10
5. What is the default return value of a function that does not explicitly return a value?
a) 0
b) None
c) False
d) Empty String
Answer
b) None
6. Which of the following is NOT a valid function name?
a) def _my_func():
b) def 2nd_function():
c) def myFunc():
d) def func_123():
Answer
b) def 2nd_function():
7. What is the correct way to call a function named greet
?
a) call greet()
b) greet()
c) run greet()
d) execute greet()
Answer
b) greet()
8. What will be the output of this code?
def my_func(a, b=10):
return a + b
print(my_func(5))
a) 15
b) 10
c) 5
d) Error
Answer
a) 15
9. What does the *args
parameter allow in a function?
a) Accepts a fixed number of arguments
b) Accepts multiple positional arguments
c) Accepts multiple keyword arguments
d) Allows a function to return multiple values
Answer
b) Accepts multiple positional arguments
10. What will be the output of this code?
def multiply(a, b=2):
return a * b
print(multiply(3, 4))
a) 12
b) 6
c) 8
d) Error
Answer
a) 12
11. What does **kwargs
do in a function?
a) Accepts multiple positional arguments
b) Accepts multiple keyword arguments
c) Accepts a fixed number of arguments
d) Throws an error if extra arguments are passed
Answer
b) Accepts multiple keyword arguments
12. What will be the output of this function call?
def example(x, y=5):
return x + y
print(example(3, y=7))
a) 10
b) 8
c) 15
d) Error
Answer
a) 10
13. What is a lambda function?
a) A function with multiple return values
b) A function with no arguments
c) A small anonymous function
d) A function that modifies global variables
Answer
c) A small anonymous function
14. What is the output of this lambda function?
square = lambda x: x * x
print(square(4))
a) 8
b) 16
c) 4
d) Error
Answer
b) 16
15. What is the correct way to pass an arbitrary number of keyword arguments?
a) def func(**kwargs):
b) def func(*kwargs):
c) def func(***kwargs):
d) def func($kwargs):
Answer
a) def func(**kwargs):
16. Which statement about Python functions is TRUE?
a) Functions must always return a value
b) A function can modify global variables without the global
keyword
c) Functions can have multiple return statements
d) Functions cannot accept multiple arguments
Answer
c) Functions can have multiple return statements
17. What will be the output of the following code?
def outer():
x = 10
def inner():
return x + 5
return inner()
print(outer())
a) 15
b) 10
c) 5
d) Error
Answer
a) 15
18. What is recursion in Python?
a) A function calling itself
b) A function returning multiple values
c) A function modifying global variables
d) A function taking no parameters
Answer
a) A function calling itself
19. What will happen if a recursive function doesn’t have a base case?
a) It will run indefinitely and cause an error
b) It will return None
c) It will stop after 10 iterations
d) It will throw a syntax error
Answer
a) It will run indefinitely and cause an error
20. What does the global
keyword do in Python?
a) Declares a variable as immutable
b) Allows a function to access a global variable
c) Restricts a function from modifying a variable
d) Creates a local variable
Answer
b) Allows a function to access a global variable
How did you do? 🎯
- 18-20 correct → 🏆 Excellent! You’re a Python functions pro!
- 14-17 correct → 👍 Great job! Keep practicing.
- 10-13 correct → 🙂 Good, but there’s room for improvement.
- Below 10 → 🤔 No worries! Review the concepts and try again.