30 Python Functions Quiz for Practice
Welcome to an ultimate Python functions quiz! Here, we have collected 30 questions based on Python functions with basic to advanced concepts. This MCQ quiz will test your Python knowledge and level up your understanding of functions.
Whether you’re a beginner or an experienced Python programmer, you should try to test your knowledge in functions. So, are you ready to score 100%? Let’s see, how well do you really understand Python functions? Letβs begin with question number 1.π
1. What keyword is used to define a function in Python?
A. define
B. func
C. def
D. function
c
In Python, the def keyword is used to define a function. For example, def my_function():
2. What is the default return value of a function that does not return anything explicitly?
A. 0
B. None
C. False
D. Undefined
b
If a function does not explicitly return a value, it returns None by default.
3. What will be the output of the following code?
def greet(): return "Scientech Easy" print(greet())
A. Scientech Easy
B. None
C. greet
D. Error
a
The function greet() returns the string “Hello”, which is then printed.
4. Which statement about Python functions is False?
A. Functions can return multiple values as a tuple.
B. Functions can be passed as arguments to other functions.
C. Functions can modify global variables without declaration.
D. Functions can be defined inside other functions.
c
To modify global variables inside a function, you need to use the global keyword. Without it, you’ll create a local variable.
5. What will be the output of the following code if no error?
def add(a, b=5): return a + b print(add(10))
A. 10
B. 15
C. 5
D. Error
b
The function add(a, b=5) has a default value of 5 for b. When you will call add(10) function, b takes the default value. Therefore, the result is 10 + 5 = 15.
6. What will happen if a function is called with more arguments than it is defined to accept?
A. It will ignore extra arguments.
B. It will use default values for missing arguments.
C. It will execute successfully.
D. It will raise a TypeError.
d
If a function is defined to accept a fixed number of arguments and is called with more, Python raises a TypeError.
7. What is the output of the following code if the function is called?
def greet(name): return f"Hello, {name}!" print(greet("Alice"))
A. Hello, Alice!
B. greet(“Alice”)
C. “Hello, {name}!”
D. Error
a
The greet() function takes a parameter name and returns a formatted string with the name inserted.
8. What is the output of this code?
def multiply(a, b=2): return a * b print(multiply(3, 4)) print(multiply(3))
A. 12, 6
B. 12, 12
C. 6, 6
D. None
a
The first function call provides both arguments (3Γ4=12), while the second call uses the default value for b (3Γ2=6).
9. What does this function return?
def func(x, y=[]): y.append(x) return y print(func(1)) print(func(2)) print(func(3, [])) print(func(4))
A. [1], [2], [3], [4]
B. [1], [1, 2], [3], [1, 2, 4]
C. [1], [2], [3], [4, 1, 2]
D. [1], [2], [3, 4], [1, 2, 4]
b
The func() function has two parameters: x and y with a default value of an empty list [ ]. If you do not provide a value to y, it uses the default list. The function appends x to y and then returns y. During the first func(1) function call, y is not provided, so it uses the default list [ ]. Now 1 is appended to the list. The list now contains [1]. Therefore, the output is [1]. During second func(2) call, again y is not provided. So, it will reuse the default list from the previous call. 2 is appended to the same list that already contains [1]. The list now contains [1, 2]. Thus, the output is [1, 2]. During the third func(3, [ ]) call, an explicitly new list ([ ]) is passed as y. 3 is appended to this new list. The list now contains [3]. Therefore, the output is [3]. During the fourth func(4) call, y is not provided. So, it again uses the same default list from the first two calls. The current default list contains [1, 2] from previous calls. 4 is appended to this list. The list now contains [1, 2, 4]. The default list y=[ ] is created only once when the function is defined, not every time the function is called. This means every time the function is called without specifying y, the same list is used.
10. What does this code output?
def outer(): x = 10 def inner(): nonlocal x x += 5 return x return inner func = outer() print(func()) print(func())
A. 15, 20
B. 10, 15
C. 15, 15
D. TypeError
a
The nonlocal keyword allows inner() function to modify x from the enclosing scope. Each call to func() function increments x by 5.
11. Which of these is not a valid function parameter specification?
A. def func(a, b, /, c, d, *, e, f)
B. def func(*, a, b, c)
C. def func(a=1, /, b=2, *, c=3)
D. def func(a, *b, c, **d)
c
Parameters before / cannot have default values because they are positional-only parameters.
12. What is the output of the below code?
def func(x): try: return x * 2 finally: return x * 3 print(func(5))
A. 10
B. 15
C. 20
D. 30
b
The finally block in Python always executes before the function returns, regardless of whether an exception occurs or not. If the finally block contains a return statement, it overrides any previous return statement in the try or except block.
13. What will be the expected output of the following code?
def func(a, b, *args, d=4): return a + b + sum(args) + d print(func(1, 2, 3, 4, d=5))
A. 10
B. 15
C. 14
D. 20
b
The function in the above code takes two required parameters (a and b), an arbitrary number of positional arguments (*args), and a keyword argument (d). The argument values of these parameters are a = 1, b = 2, args = (3,4), d = 5. So, the output is 1 + 2 + (3 + 4) + 5 = 15.
14. What will be the output of the following lambda function?
square = lambda x: x ** 2 print(square(5))
A. 10
B. 25
C. 5
D. TypeError
b
The lambda function takes a number x and returns its square (5Β² = 25).
15. Which of these is the correct syntax for a lambda function?
A. lambda x: return x + 1
B. function(x): lambda x + 1
C. def lambda(x): x + 1
D. lambda x: x + 1
d
Lambda functions use the syntax lambda arguments: expression. Don’t use the return keyword.
16. What will be the expected output of the following code if no error?
def modify_list(lst): lst.append(4) my_list = [1, 2, 3] modify_list(my_list) print(my_list)
A. [1, 2, 3]
B. [1, 2, 3, 4]
C. None
D. Error
b
Lists are mutable, so modifying lst inside the function affects my_list.
17. What will be the expected output of the following code if the function is called?
def test(a, b, c=5, *args, d=10, **kwargs): return a + b + c + d + sum(args) + sum(kwargs.values()) print(test(1, 2, 3, 4, 5, d=6, x=7, y=8))
A. 40
B. 36
C. 30
D. 42
b
a = 1, b = 2, c = 3, args = (4, 5), d = 6, kwargs = {‘x’: 7, ‘y’: 8}. The output is 1 + 2 + 3 + (4+5) + 6 + (7+8) = 36.
18. Will the code successfully execute? If yes, what will be the expected output?
def f(x, y=x+1): return y print(f(5))
A. 5
B. 6
C. TypeError
D. NameError
d
The f() function tries to use x as a default value before it is defined. So, it will cause an error named NameError.
19. What does this lambda function do?
check = lambda x: "Even" if x % 2 == 0 else "Odd" print(check(7))
A. Returns True if even, False if odd
B. Returns “Even” or “Odd” based on input
C. Raises an error
D. Returns the number squared
b
This is a ternary operation in a lambda that checks even/odd status.
20. What will be the expected output of the following Python code?
def func(x, y={}): y[x] = x + 1 return y a = func(1) b = func(2, {}) c = func(3) print(a, b, c)
A. {1: 2} {2: 3} {3: 4}
B. {1: 2, 3: 4} {2: 3} {1: 2, 3: 4}
C. {1: 2} {2: 3} {1: 2, 3: 4}
D. Error
b
The function func() takes two parameters: x and y where x is a required argument and y is an optional argument with a default value of an empty dictionary {}. This function adds a new key-value pair to y where x is the key and x + 1 is its value. The updated dictionary y is then returned. During the first func(1) function call, y is not provided. So, the function uses the default dictionary {}. The dictionary is updated with {1: 2}, which is stored in a. Thus, the output is a = {1: 2}. During the second func(2, {}) call, an explicitly new dictionary {} is passed. The dictionary is updated with {2: 3}, and is stored in b. The output is b = {2: 3}. With the third func(3) call, y is not provided. So, it will reuse the default dictionary {1: 2} from the first call. The updated dictionary is {1: 2, 3: 4}, which is stored in c. The default dictionary {} is created once when the function is defined, not every time the function is called. This means every time the function is called without specifying y, the same dictionary is used.
21. What is the expected output of the following program code?
def func(x, y=None): if y is None: y = [] y.append(x) return y print(func(1)) print(func(2)) print(func(3, [])) print(func(4))
A. [1], [1, 2], [3], [1, 2, 4]
B. [1], [1, 2], [3], [1, 2, 3, 4]
C. [1], [2], [3], [4]
D. None of the above
c
The function func() has two parameters: x and y, where x is a required argument and y is an optional argument with a default value of None. Inside the function, if y is None, a new empty list [ ] is created. x is appended to y and returns y. During 1st func(1) call, y is not provided. So, it defaults to None. A new list is created and 1 is appended to y. So, y = [1]. The output is [1]. During 2nd func(2) call, y is not provided. So, it defaults to None again. A new list is created again and 2 is appended to y. So, y = [2]. The output is [2]. This process is the same for third, fourth function call.
22. If there is no error in the below code, then what will be the output?
def func(x, y=x): return y print(func(5))
A. 5
B. TypeError
C. NameError
D. None
c
Default arguments are evaluated when the function is defined, not when it is called. x is not defined at the time of default argument evaluation.
23. What is the output of this program code?
funcs = [lambda x: x + i for i in range(3)] print(funcs[0](10))
A. 10
B. 11
C. 12
D. 13
c
The above code creates a list of lambda functions. The variable i iterates over range(3), which means i = 0, 1, 2. The lambda function inside the list comprehension is: lambda x: x + i where i is supposed to be taken from the loop. In Python, lambda functions inside list comprehensions capture variables by reference, not by value. This means that all lambda functions will use the final value of i, which is 2 after the loop completes. This is called late binding, which means the lambda uses the latest value of the variable when called, not when defined. Thus, the lambda x: x + 2. When the funcs[0](10) is called, funcs[0] refers to lambda x: x + 2. The output is 10 + 2 = 12.
24. What will this Python code print on the console?
def add(a, b): return a + b def sub(a, b): return a - b result = [add, sub] print(result[0](5, result[1](8, 3)))
A. 10
B. 11
C. 12
D. 13
a
The function add(a, b) returns the sum of a and b. The sub(a, b) returns the difference of a and b. Then, we are storing functions in a list. The line result = [add, sub] means result[0] refers to add, and result[1] refers to sub. Thus, the print(result[0](5, result[1](8, 3))) is equivalent to print(add(5, sub(8, 3))). Evaluating sub(8, 3) β 8 – 3 = 5. Now the expression becomes print(add(5, 5)). Evaluating add(5, 5) β 5 + 5 = 10.
25. What will be the output of the following recursive function?
def factorial(n): if n == 0: return 1 return n * factorial(n - 1) print(factorial(5))
A. 5
B. 15
C. 120
D. 25
c
The function calculates the factorial of 5 using recursion: 5! = 5 Γ 4 Γ 3 Γ 2 Γ 1 = 120
26. What is the base case in the following recursive function?
def sum_numbers(n): if n == 1: return 1 return n + sum_numbers(n - 1)
A. if n == 0:
B. if n == 1:
C. return n + sum_numbers(n – 1)
D. return n
b
The base case is the condition that stops recursion. Here, when n == 1, the function returns 1 instead of calling itself again.
27. What will be the output of the following program code?
def func(n): if n <= 0: return 0 return n + func(n - 2) print(func(5))
A. 5
B. 7
C. 9
D. 12
c
This function sums numbers in a decreasing pattern: 5 + 3 + 1 = 9
28. What will be the output of this recursive function?
def fun(n): if n > 0: fun(n - 1) print(n, end=" ") fun(3)
A. 3 2 1
B. 1 2 3
C. 3 2 1 0
D. 0 1 2 3
b
The function first calls itself (fun(n-1)) before printing. This means that it reaches the base case (n = 0) first, then prints numbers on the way back.
29. What does the following function compute?
def f(n): if n <= 1: return n return f(n - 1) + f(n - 2)
A. Factorial
B. Sum of numbers
C. Fibonacci sequence
D. None of the above
c
This function follows the Fibonacci formula: F(n) = F(n-1) + F(n-2), which defines Fibonacci numbers.
30. What is the expected output of this program code?
def decorator(func): def wrapper(*args): return func(*args) * 2 return wrapper @decorator def add(a, b): return a + b print(add(2, 3))
A. 5
B. 10
C. 25
D. TypeError
b
The decorator modifies the add function to return double its original result (5 Γ 2 = 10).
Quiz Results
0
Total Questions
0
Correct Answers
0
Incorrect Answers
0%
Score