Python Functions Quiz - Test your skills
Take this Quiz and challenge your understanding of function concepts, lambda expressions, default arguments, recursion, and more. The questions test both conceptual and practical understanding of Python functions.
Question 1.
What is the primary purpose of a function?
Question 2.
Which keyword is used to define a function in Python?
Question 3.
Which value is returned by default if a function does not have a return
statement.?
Question 4.
What is the default scope of a variable defined inside a function?
Question 5.
What is the output of the following code?
def my_func():
a = 10
my_func()
print(a)
Question 6.
How can you make a variable that is local to a function accessible globally.?
Question 7.
The following code raises a NameError
exception, add a single line to it to make it work without altering the existing lines.
Your answer:
Question 8.
In the following code, we want the printed value( of x
) to be 20
rather than 0. Add nonlocal
statement to the code to make it work as expected without altering existing lines.
Your answer:
Question 9.
What is the main purpose of the return
statement in a function.?
Question 10.
What is the purpose of *args
in function definition?
Question 11.
What is the difference between *args
and **kwargs
in function definition?
Question 12.
What will be the output of the following code.?
def func(a, *args, **kwargs):
print(args, end = ', ')
print(kwargs)
func(0, 10, 20, x = 100, y = 200)
Question 13.
What is the output of the following code.?
def add(a, b, /):
print(a + b)
add(a = 20, b = 10)
Question 14.
What is the key difference between a method and a function.?
Question 15.
Which of the following conditions must be met for recursion to work correctly?
Question 16.
What is the difference between a generator function and a normal function?
Question 17.
What is a lambda function in Python?
Question 18.
Create a lambda function and assign it to a variable add
. The function should take two numbers, a
and b
as arguments and returns their sum. Don't alter existing line.
Your answer:
Question 19.
What is a higher-order function?
Question 20.
Which of the following built-in functions is a higher-order function.?
Question 21.
What is the primary purpose of a decorator in Python?
Question 22.
Which symbol is used to apply a decorator to a function?
Question 23.
In the following code, the add()
function simply returns the sum of two numbers.
We want to create a decorator which after applying to add, the returned result will be in the form "a + b = sum". For example add(10, 20)
should return "10 + 20 = 30"
.
Your task:
- Finish implementation of add_decorator(). With at most 3 lines of code.
- Apply
add_decorator()
to functionadd()
. - Don't alter existing lines of code.
Your answer: