Quiz Overview
This chapter quiz consolidates everything you learned about functions in Python.
- Format: Four sections. Questions display one section at a time after you start.
- Timer: 45 minutes total. The timer cannot be paused or reset once the quiz starts.
- Scoring:
- MCQ questions may award partial points for each correct option.
- Coding questions award points based on test case pass rate.
- Your final score and percentage are computed automatically when you submit or when time runs out.
Python Fundamentals - Chapter 4: Functions
Quiz Complete!
Section: Function Definition & Arguments
What is the output of the following code?
5 Points1def greet(name, msg="Hello"):2 return f"{msg}, {name}!"3
4print(greet("Alice"))Hello, {name}!
Hello, Alice!
Error: missing required argument
None
What is the output of the following code?
5 Points1def calculate(x, y, /, operation="add", *, validate=True):2 if validate:3 if operation == "add":4 return x + y5 return x * y6 return None7
8result = calculate(5, 3, operation="multiply")9print(result)8
None
15
Error
What is the output of the following code?
5 Points1def sum_all(*args):2 total = 03 for num in args:4 total += num5 return total6
7print(sum_all(1, 2, 3, 4, 5))15
(1, 2, 3, 4, 5)
Error
1
What is the output of the following code?
5 Points1def process_data(title, **kwargs):2 result = f"{title}: "3 for key, value in kwargs.items():4 result += f"{key}={value}, "5 return result[:-2]6
7print(process_data("User", name="Alice", age=25))User: name=Alice, age=25,
User: {'name': 'Alice', 'age': 25}
Error
User: name=Alice, age=25
Which of the following function calls will cause a TypeError?
5 Points1def test(x, y, /, z, *, w):2 return x + y + z + wtest(x=1, y=2, z=3, w=4)
test(1, 2, 3, w=4)
test(1, 2, z=3, w=4, x=5)
test(1, 2, z=3, w=4)
Function with Default Arguments
15 PointsWrite a function called calculate that takes three parameters: x, y, and operation (with default value “add”). If operation is “add”, return x + y. If operation is “multiply”, return x * y. Otherwise, return x - y.
Read two integers x and y from input, call the function with these values (using default operation), and print the result.
Function with Default Arguments
Solution Approach:
Define a function with a default argument for operation. Use conditional logic to perform the appropriate operation based on the parameter value.
Example Solution:
1def calculate(x, y, operation="add"):2 if operation == "add":3 return x + y4 elif operation == "multiply":5 return x * y6 else:7 return x - y8
9x = int(input())10y = int(input())11result = calculate(x, y)12print(result)Sum with *args
15 PointsWrite a function called sum_numbers that takes any number of arguments using *args and returns their sum.
Read an integer n from input, then read n integers. Call the function with these integers (unpacking them) and print the result.
Sum with *args
Solution Approach:
Use *args to accept variable number of arguments. Read n, then read n numbers into a list, and unpack the list when calling the function.
Example Solution:
1def sum_numbers(*args):2 return sum(args)3
4n = int(input())5numbers = []6for i in range(n):7 numbers.append(int(input()))8result = sum_numbers(*numbers)9print(result)Section: Scope & Variable Access
What is the output of the following code?
5 Points1x = 102
3def modify():4 x = 205 return x6
7print(modify())8print(x)20\n20
20\n10
10\n10
Error
What is the output of the following code?
5 Points1count = 02
3def increment():4 global count5 count += 16 return count7
8increment()9increment()10print(count)0
1
2
Error
What is the output of the following code?
5 Points1def outer():2 x = 103 def inner():4 nonlocal x5 x = 206 inner()7 return x8
9print(outer())20
10
Error
None
What is the output of the following code?
5 Points1x = 52
3def func1():4 x = 105 def func2():6 print(x)7 func2()8
9func1()10print(x)5\n5
10\n10
5\n10
10\n5
Global Counter
15 PointsWrite a program that uses a global variable counter initialized to 0. Create a function increment() that uses the global keyword to increment the counter and return its value.
Read an integer n from input. Call increment() n times, printing the result each time (each on a new line).
Global Counter
Solution Approach:
Declare a global variable, then use the global keyword inside the function to modify it.
Example Solution:
1counter = 02
3def increment():4 global counter5 counter += 16 return counter7
8n = int(input())9for i in range(n):10 print(increment())Section: Advanced Function Concepts
What is the output of the following code?
5 Points1def create_multiplier(factor):2 def multiplier(number):3 return number * factor4 return multiplier5
6double = create_multiplier(2)7print(double(5))7
5
10
Error
What is the output of the following code?
5 Points1operations = {2 'add': lambda x, y: x + y,3 'multiply': lambda x, y: x * y4}5
6result = operations['add'](3, 4)7print(result)12
7
lambda x, y: x + y
Error
What is the output of the following code?
5 Points1def apply(func, value):2 return func(func(value))3
4def square(x):5 return x ** 26
7print(apply(square, 3))81
9
6
Error
What is the output of the following code?
5 Points1def create_counter():2 count = 03 def counter():4 nonlocal count5 count += 16 return count7 return counter8
9c1 = create_counter()10c2 = create_counter()11print(c1(), c2(), c1())1 1 1
1 2 3
1 1 2
Error
Closure: Power Function
15 PointsWrite a function called create_power_function that takes an exponent parameter and returns a function. The returned function should take a base parameter and return base ** exponent.
Read two integers: exponent and base. Create a power function using create_power_function with the exponent, then call it with the base and print the result.
Closure: Power Function
Solution Approach:
Create a closure that captures the exponent from the outer function and uses it in the inner function.
Example Solution:
1def create_power_function(exponent):2 def power(base):3 return base ** exponent4 return power5
6exponent = int(input())7base = int(input())8power_func = create_power_function(exponent)9print(power_func(base))Section: Built-in Functions & Documentation
What is the output of the following code?
5 Points1numbers = [3, 1, 4, 1, 5, 9, 2]2result = max(numbers, key=lambda x: -x)3print(result)9
1
[1, 1]
Error
What is the output of the following code?
5 Points1numbers = [1, 2, 3, 4, 5]2squared = list(map(lambda x: x ** 2, numbers))3print(squared)[1, 2, 3, 4, 5]
<map object at 0x...>
[1, 4, 9, 16, 25]
Error
What is the output of the following code?
5 Points1numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]2evens = list(filter(lambda x: x % 2 == 0, numbers))3print(evens)[2, 4, 6, 8, 10]
[1, 3, 5, 7, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[]
What is the output of the following code?
5 Points1def greet(name):2 """This function greets a person by name"""3 return f"Hello, {name}!"4
5print(greet.__doc__)None
greet
Error
This function greets a person by name
Using map() and filter()
15 PointsRead an integer n, then read n integers. Use filter() to get only even numbers, then use map() to square each even number. Print the sum of the squared even numbers, then print the maximum squared even number (or 0 if there are no even numbers).
Example: If input is 5 followed by 1, 2, 3, 4, 5, the even numbers are [2, 4], squared they become [4, 16]. Sum is 20, max is 16.
Using map() and filter()
Solution Approach:
Use filter() with a lambda to get even numbers, then map() to square them. Calculate sum and max of the results.
Example Solution:
1n = int(input())2numbers = []3for i in range(n):4 numbers.append(int(input()))5
6evens = list(filter(lambda x: x % 2 == 0, numbers))7squared = list(map(lambda x: x ** 2, evens))8
9if squared:10 print(sum(squared))11 print(max(squared))12else:13 print(0)14 print(0)Function with Docstring
15 PointsWrite a function called multiply that takes two parameters a and b and returns their product. Include a docstring that says “Multiply two numbers and return the result.”
Read two integers from input, call the function, and print the result.
Function with Docstring
Solution Approach:
Define a function with a docstring (triple-quoted string as the first statement) and return the product.
Example Solution:
1def multiply(a, b):2 """Multiply two numbers and return the result."""3 return a * b4
5a = int(input())6b = int(input())7result = multiply(a, b)8print(result)After You Finish
Great work completing the quiz! Review your score and identify areas for improvement:
- >= 80%: Excellent! You have a solid understanding of functions. Ready to move on.
- 60-79%: Good foundation. Revisit missed topics and practice more exercises before advancing.
- < 60%: Review the chapter materials, especially the concepts you missed. Practice with more coding exercises.
Functions are fundamental to Python programming. Mastery of functions enables you to write modular, reusable, and maintainable code.
