Quiz — Chapter 6: Functions

Answer key at end.


Multiple Choice

Q1. What keyword is used to define a function in Python?

a) function b) define c) def d) fn


Q2. What does the following function return?

def add(a, b):
    result = a + b

a) a + b b) result c) None d) 0


Q3. In def f(x, y=10):, what is y=10?

a) A keyword argument b) A default parameter c) A local variable assignment d) An optional return value


Q4. What is a docstring?

a) A comment using # inside a function b) A string literal as the first statement in a function, describing what it does c) The documentation website for a function d) A type annotation for function parameters


Q5. What is the output of this code?

x = 10
def modify():
    x = 20
    return x
print(modify())
print(x)

a) 20 then 20 b) 20 then 10 c) 10 then 10 d) NameError


Q6. Which of the following is a valid lambda function that doubles its input?

a) lambda x: x * 2 b) def lambda(x): return x * 2 c) lambda(x) => x * 2 d) fn x: x * 2


Q7. What does the following return?

def metrics(revenue, cogs):
    gp = revenue - cogs
    margin = gp / revenue
    return gp, margin

a, b = metrics(100000, 60000)

a) a = 40000, b = 0.4 b) a = (40000, 0.4), b = undefined c) a = 0.4, b = 40000 d) TypeError


Q8. When importing a function from a module, which syntax is correct?

a) import calculate_gross_margin from business_math b) from business_math import calculate_gross_margin c) include business_math.calculate_gross_margin d) using business_math.calculate_gross_margin


Q9. What is the DRY principle?

a) Don't Run Yourself — avoid infinite loops b) Don't Repeat Yourself — every piece of logic should exist in one place c) Define Reusable Yield — functions should use generators d) Don't Reassign Yourself — avoid variable reassignment


Q10. In the context of the chapter, what is a "pure function"?

a) A function with no parameters b) A function that only uses numbers, not strings c) A function that takes inputs and returns a value without modifying any external state d) A function defined in a separate module file


True or False

Q11. Parameters with default values must come before parameters without defaults in the function definition.

Q12. A function can return multiple values in Python.

Q13. Lambda functions can contain if/else blocks and for loops.

Q14. Variables defined inside a function are accessible outside it.

Q15. A docstring is stored in the function's __doc__ attribute.

Q16. You can pass arguments to a function by name (keyword arguments), regardless of their position.

Q17. The global keyword inside a function allows you to read a global variable.

Q18. A function that prints something but has no return statement implicitly returns None.

Q19. Calling help(my_function) in Python displays the function's docstring.

Q20. A module is a Python file that contains functions and can be imported.


Short Answer

Q21. What is the difference between def f(price, rate=0.10) and calling f(price=100, rate=0.15) — which uses parameters, which uses arguments?

Q22. Write a one-line lambda function that takes price and rate and returns price * (1 - rate).

Q23. Give two specific business scenarios where a function is clearly better than inline code.


Answer Key

Multiple Choice: Q1: c | Q2: c | Q3: b | Q4: b | Q5: b | Q6: a | Q7: a | Q8: b | Q9: b | Q10: c

True or False: Q11: False (parameters WITH defaults must come AFTER parameters without) Q12: True (using tuple packing) Q13: False (lambdas are single expressions — no statements like if blocks or for loops) Q14: False (local variables exist only inside the function) Q15: True Q16: True Q17: False (you can READ globals without the keyword; global is needed to WRITE them) Q18: True Q19: True Q20: True

Short Answer:

Q21: price and rate=0.10 are parameters (in the function definition). price=100 and rate=0.15 are keyword arguments (passed when calling the function).

Q22: lambda price, rate: price * (1 - rate)

Q23: Any two valid scenarios — e.g., (1) a discount calculation used in 5 different scripts — define once as a function; (2) a tax calculation that changes when tax law changes — update one function rather than hunting through all scripts.