🧮 Unit 3: Modular Programming & Recursion — Practice Quiz

Assessment AS Learning — Self-Check
🔄 Not Graded — Unlimited Retakes
Purpose: Self-check on functions, parameters, scope, and recursion. Instant feedback. Python 3.
Score: 0 / 12
Question 1 — Function call output
What is printed?
def add(a, b): return a + b x = add(3, add(2, 5)) print(x)
Solution:
Inner: add(2,5)=7. Outer: add(3,7)=10.
Question 2 — Default argument
What is the output?
def greet(name, greeting="Hello"): return f"{greeting}, {name}!" print(greet("Ada")) print(greet("Bob", "Hi"))
Solution:
Line 1: Hello, Ada!. Line 2: Hi, Bob!. The default value "Hello" is used only when the second arg is omitted.
Question 3 — Recursion: factorial
What does factorial(4) return?
def factorial(n): if n <= 1: return 1 return n * factorial(n - 1)
Solution:
4! = 4 × 3 × 2 × 1 = 24.
Question 4 — Identify the base case
In a recursive function, what is the role of the base case?
Solution:
The base case is the simplest case the function can solve directly without further recursion. Without it, the function calls itself forever and crashes with a stack overflow.
Question 5 — Scope
What is printed?
x = 10 def f(): x = 5 print(x) f() print(x)
Solution:
Inside f, x = 5 creates a local x that shadows the global. After the function returns, the global x is unchanged. Output: 5 then 10.
Question 6 — Mutating a list parameter
What is printed?
def add_one(lst): lst.append(1) a = [10, 20] add_one(a) print(a)
Solution:
Lists are mutable; lst inside the function refers to the same list as a. .append mutates the list in place, so a is now [10, 20, 1].
Question 7 — Recursive sum
What does rsum(4) return?
def rsum(n): if n == 0: return 0 return n + rsum(n - 1)
Solution:
4 + 3 + 2 + 1 + 0 = 10.
Question 8 — Counting Fibonacci calls
For the naive recursive Fibonacci, fib(5) calls fib a total of how many times (including the original call)?
def fib(n): if n < 2: return n return fib(n-1) + fib(n-2)
Solution:
Total calls follow the recurrence T(n)=T(n-1)+T(n-2)+1 with T(0)=T(1)=1. So T(2)=3, T(3)=5, T(4)=9, T(5)=15. The exponential blow-up is why memoization matters.
Question 9 — Modularization
Which is the BEST reason to break a 200-line program into multiple short functions?
Solution:
The principle is modularity — small, single-purpose functions are easier to test, read, and reuse. (a), (c), (d) are false.
Question 10 — Return vs. print
What does this print?
def double(x): print(x * 2) result = double(7) print(result)
Solution:
Line 1: 14 (printed inside the function). Line 2: Nonedouble has no return statement, so it implicitly returns None.
Question 11 — Recursive string reversal
What does reverse("dog") return?
def reverse(s): if len(s) <= 1: return s return reverse(s[1:]) + s[0]
Solution:
reverse("dog") = reverse("og") + "d" = (reverse("g") + "o") + "d" = ("g" + "o") + "d" = "god".
Question 12 — When to use recursion
Which problem is BEST suited to a recursive solution?
Solution:
Recursion shines on naturally recursive (self-similar) data structures — here, a tree of folders. (a), (b), (d) are flat / sequential and a simple loop is preferred.