Recursive Function Patterns Quiz Quiz

Challenge your understanding of common recursive function patterns, principles, and behaviors with these scenario-based multiple-choice questions. This quiz focuses on recursive logic, base and recursive cases, and examples from programming and problem-solving contexts.

  1. Identifying the Base Case

    Which statement best describes the base case in the following recursive function for computing the factorial of a number: if n == 0, return 1; else return n * factorial(n-1)?

    1. The condition n == 0 that returns 1
    2. The step where n is decremented by 1
    3. The multiplication of n and factorial(n-1)
    4. The repeated calling of the function with smaller n

    Explanation: The base case in any recursion is the condition upon which recursion terminates, preventing infinite calls. Here, n == 0 returning 1 fulfills that role. Decrementing n is part of progressing toward the base case but is not the base case itself. Multiplication and repeated function calls describe the recursive step, not the base case.

  2. Recursive Output Sequence

    If a function recursively prints the value of n and then calls itself with n-1 until n reaches 1, what is the output for n = 3?

    1. 1 2 3
    2. 3 1 2
    3. 3 2 1
    4. 2 3 1

    Explanation: Recursion processes each call before moving to the next, so printing n before the recursive call outputs 3, then 2, then 1 as each call executes. The sequence 1 2 3 would occur if the print was after the recursion. The other options represent either unordered or incorrect sequences.

  3. Recursive Case Recognition

    In a recursive function for calculating the nth term of a sequence where term(n) = term(n - 1) + term(n - 2), which statement best identifies the recursive case?

    1. term(0) = 0
    2. Returning 0 when n is zero
    3. Stopping when the sequence is negative
    4. term(n) = term(n - 1) + term(n - 2)

    Explanation: The recursive case is where the function definition refers to itself with smaller arguments—in this case, combining values from previous terms. Defining term(0) or returning 0 for n equals zero are base cases, not recursive ones. Stopping for negative values is sometimes used for error handling but not required for standard recursive cases.

  4. Infinite Recursion Scenario

    Which of the following modifications will most likely cause infinite recursion in a countdown function that stops when n == 0?

    1. Decrementing n by 2 instead of 1
    2. Omitting the check for n == 0
    3. Starting with n = 0
    4. Printing n before the recursive call

    Explanation: Without a proper base case check, such as n == 0, the function never stops calling itself and results in a stack overflow or infinite loop. Decrementing by 2 still eventually reaches 0 if n is even. Printing before recursion does not affect termination. Starting at zero triggers the base case immediately if present.

  5. Recursive Approach Use Case

    Which of the following problems is best solved using a recursive function pattern?

    1. Reading one input value
    2. Sorting a simple array with only two elements
    3. Adding two numbers
    4. Calculating the nth Fibonacci number

    Explanation: The Fibonacci sequence is naturally defined recursively as each term depends on the two preceding terms. Sorting two elements or adding numbers doesn't need recursion and is inefficient for that purpose. Reading a single input value is a straightforward action requiring no recursion.