📝 Unit 3: Modular Programming & Recursion — Unit Test
Assessment OF Learning — Summative
✅ Graded — Counts Toward 70% Term Mark
⏱️ Duration: 75 minutes | Total: /60 marks
Show all work. Code answers must be syntactically valid Python 3. No internet, no AI tools.
K/U
/15
Thinking
/15
Comm.
/15
Applic.
/15
Part A: Knowledge & Understanding [15 marks]
1. [2 marks]
What does this print?
def mystery(a, b=2, c=1):
return a * b + c
print(mystery(3))
print(mystery(3, 4))
print(mystery(3, c=5))
2. [3 marks]
Trace this recursive function. Show every recursive call until the base case, then the value returned at each level. What is power(2, 5)?
def power(base, exp):
if exp == 0:
return 1
return base * power(base, exp - 1)
3. [2 marks]
Identify two distinct types of parameter passing seen in Python (e.g., positional vs. keyword) and give a one-line example of each.
4. [2 marks]
What is the output?
x = 1
def f(x):
x = x + 1
return x
print(f(x))
print(x)
5. [3 marks]
A recursion has two essential parts. Name them and explain the danger of getting either wrong.
6. [3 marks]
Write the simplest Python function is_palindrome(s) that returns True when the string reads the same forwards and backwards (case-insensitive). You may use slicing.
Part B: Thinking & Investigation [15 marks]
7. [5 marks]
Find & fix the bugs. The recursive function below is supposed to compute the sum of digits in a positive integer (e.g., sum_digits(123) = 6). It has two bugs.
def sum_digits(n):
if n = 0:
return 1
return (n % 10) + sum_digits(n // 10)
8. [5 marks]
Convert this iterative function to a recursive one. Show both versions and confirm they return the same answer for n = 5.
def count_down_iter(n):
while n > 0:
print(n)
n -= 1
print("Blastoff!")
9. [5 marks]
A team is asked to design a function checkout_total(cart, tax_rate). The cart is a list of (name, price, quantity) tuples. The function should return the after-tax total (rounded to 2 decimal places). Decompose the problem into at least three helper functions, list their signatures, and write the orchestrating checkout_total in terms of those helpers.
Part C: Communication [15 marks]
10. [5 marks]
Explain in your own words: what is variable scope? Use the LEGB rule (Local, Enclosing, Global, Built-in) and a small code example.
11. [5 marks]
A peer says: "Recursion is just iteration with extra steps — you can always rewrite a loop with recursion or vice versa, so it doesn't matter." Write a thoughtful response (3–5 sentences) discussing trade-offs: clarity, performance, stack-depth limits, and naturalness for tree/divide-and-conquer problems.
12. [5 marks]
Write a complete docstring for the function below following Python conventions (Description, Parameters, Returns, Example).
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
Part D: Application [15 marks]
13. [5 marks]
Write a recursive function count_letter(s, ch) that returns the number of times the character ch appears in the string s. Do not use loops or .count(). Show the result for count_letter("banana", "a").
14. [5 marks]
Top-down design problem. A teacher wants a small program that:
Reads a list of marks (one per line) from a file marks.txt.
Drops the lowest mark.
Reports the average of the remaining marks.
Write the function main() that orchestrates this work, plus the three helper functions read_marks(path), drop_lowest(marks), and average(marks). Each helper should do one thing.
15. [5 marks]
Tower-of-Hanoi style problem. Write a recursive function moves(n) that returns the minimum number of moves required to transfer n disks in the Tower of Hanoi (formula: 2ⁿ - 1; you must derive it via recursion, not hard-code). Show moves(3) and moves(5).