Quiz Details

Sign Up to Download PDF

QZ-20260506-24360

Topics:
Functions in python
Difficulty: Level 4 - Hard
Questions: 15
Language: English (English)
Generated: May 06, 2026 at 03:28 PM
Generated by: NANZIRI PATIENCE

Instructions: Select an answer for each question and click "Check Answer" to see if you're correct. Then view the explanation to learn more!
1 What is the output of the following code: `def f(x): return x**2; print(f(3))`?
Correct Answer: C
Explanation: The function `f` squares the input, so `f(3)` returns 3 squared, which is 9.
2 What does the keyword 'global' allow you to do within a function?
Correct Answer: B
Explanation: Using 'global' allows a function to modify a variable defined at the module level, providing access to the global variable.
3 Which of the following will raise an error when called?
Correct Answer: D
Explanation: The function `func` requires two arguments, and calling it with only one argument will raise a TypeError.
4 What will be the output of the following code: `def greet(name): return f'Hello, {name}!'; print(greet('Alice'))`?
Correct Answer: B
Explanation: The function `greet` formats a string with the name provided, resulting in 'Hello, Alice!'.
5 What is the purpose of the *args syntax in a function definition?
Correct Answer: A
Explanation: '*args' allows you to pass a variable number of positional arguments to a function, which are accessible as a tuple.
6 Which of the following statements is true regarding lambda functions in Python?
Correct Answer: B
Explanation: Lambda functions in Python are limited to a single expression, making them concise for simple operations.
7 What is the output of the following code snippet: `def add(x, y=5): return x + y; print(add(2))`?
Correct Answer: D
Explanation: The function adds the first argument (2) to the default value of the second argument (5), resulting in 7.
8 What will be the output of the following code: `def func(a, b=2, c=3): return a + b + c; print(func(1, c=4))`?
Correct Answer: A
Explanation: The function adds 1 (a) + 2 (default b) + 4 (provided c), resulting in 7.
9 In Python, what does the term 'higher-order function' refer to?
Correct Answer: C
Explanation: A higher-order function is one that can take other functions as arguments or return them as results.
10 What is the output of the following code: `def foo(a, b=[]): b.append(a); return b; print(foo(1)); print(foo(2))`?
Correct Answer: C
Explanation: The list `b` is mutable and retains its state between function calls, so the second call appends to the same list.
11 What is the purpose of the `return` statement in a function?
Correct Answer: A
Explanation: The `return` statement exits the function and sends a value back to where the function was called.
12 What will be the output of the following code: `def mystery(a, b): return a if a > b else b; print(mystery(10, 20))`?
Correct Answer: D
Explanation: The function returns the greater of the two arguments, and since 20 is greater than 10, it returns 20.
13 Which of the following correctly defines a function that returns the square of a number?
Correct Answer: B
Explanation: All provided options are valid ways to define a function that returns the square of a number in Python.
14 In Python, how can a function provide multiple return values?
Correct Answer: D
Explanation: A function can return multiple values using any of these data structures, which can then be unpacked.
15 What will be the output of the following code: `def outer(): def inner(): return 'Inner'; return inner; print(outer()())`?
Correct Answer: D
Explanation: The `outer` function returns the `inner` function, which is then called immediately, returning 'Inner'.
Want to generate another quiz?
Generate New Quiz Browse All Quizzes
Home Quizzes Create Cards Account