Recursion FRQ Practice

Responses receive AI feedback against a rubric after submission. Scores are practice estimates.

Question 1 of 5

A badge printer uses reverse on the letters "be".

public static String reverse(String s)
{
    if (s.length() <= 1)
    {
        return s;
    }
    return reverse(s.substring(1)) + s.substring(0, 1);
}

(a) List every reverse call in the order it begins, including the original call, and the argument of each. (b) State the return value of each of those calls. (c) State the value of reverse("be").

Question 2 of 5

A pair of trail forks is modeled by pair. Complete the call tree for pair(3).

public static int pair(int n)
{
    if (n <= 1)
    {
        return 1;
    }
    return pair(n - 1) + pair(n - 2);
}
Call Makes these calls Returns
pair(3) pair(2) and pair(1)
pair(2) pair(1) and pair(0)
pair(1) none (base)
pair(0) none (base)

(a) Fill the return column, counting that some calls occur more than once. (b) State the value of pair(3). (c) State how many times pair is called in total during pair(3), including the original call.

Question 3 of 5

A clerk traces sumDigits(472).

public static int sumDigits(int n)
{
    if (n < 10)
    {
        return n;
    }
    return n % 10 + sumDigits(n / 10);
}

(a) For each call, state the parameter n and the value that call returns. (b) State how many calls occur, including the original. (c) In words, describe an equivalent iterative process that produces the same result. Do not write a new recursive method.

Question 4 of 5

A chime method prints on the way down and on the way back.

public static void mirror(int n)
{
    if (n <= 0)
    {
        return;
    }
    System.out.print(n);
    mirror(n - 1);
    System.out.print(n);
}

(a) State the text displayed by mirror(3). (b) Explain why the first 3 is printed before any 2, using the order of statements in one call. (c) Explain that the n in mirror(3) is not the same variable as the n in mirror(2).

Question 5 of 5

Compare countdown(2) with countup(2). Recursion is another form of repetition.

public static void countdown(int n)
{
    if (n == 0)
    {
        System.out.print("go");
    }
    else
    {
        System.out.print(n);
        countdown(n - 1);
    }
}

public static void countup(int n)
{
    if (n == 0)
    {
        return;
    }
    countup(n - 1);
    System.out.print(n);
}
Method call Text displayed
countdown(2)
countup(2)

(a) Fill the table. (b) Identify the base case in each method. (c) In words, describe how a loop could display the same countdown(2) text. Do not write a recursive method.