Recursion MCQ Practice

Question 1 of 10

A fireworks booth uses this method to scale a spark count. What value does product(4) return?

public static int product(int n)
{
    if (n <= 1)
    {
        return 1;
    }
    return n * product(n - 1);
}
  1. A. 4
  2. B. 10
  3. C. 24
  4. D. 1

Question 2 of 10

A starter horn prints a countdown. What text does countdown(3) display?

public static void countdown(int n)
{
    if (n == 0)
    {
        System.out.print("go");
    }
    else
    {
        System.out.print(n);
        countdown(n - 1);
    }
}
  1. A. go321
  2. B. 321go
  3. C. 123go
  4. D. 321

Question 3 of 10

A parcel desk adds the digits of a routing number. What value does sumDigits(472) return?

public static int sumDigits(int n)
{
    if (n < 10)
    {
        return n;
    }
    return n % 10 + sumDigits(n / 10);
}
  1. A. 13
  2. B. 472
  3. C. 4
  4. D. 2

Question 4 of 10

A trail sign is flipped by this method. What value does reverse("oak") return?

public static String reverse(String s)
{
    if (s.length() <= 1)
    {
        return s;
    }
    return reverse(s.substring(1)) + s.substring(0, 1);
}
  1. A. oak
  2. B. kao
  3. C. ako
  4. D. o

Question 5 of 10

A lane marshal prints after the recursive call returns. What text does countup(3) display?

public static void countup(int n)
{
    if (n == 0)
    {
        return;
    }
    countup(n - 1);
    System.out.print(n);
}
  1. A. 321
  2. B. 3
  3. C. 123
  4. D. 0123

Question 6 of 10

Using the product method from a spark-count helper, how many times is product called in total during the evaluation of product(4), including the original call?

public static int product(int n)
{
    if (n <= 1)
    {
        return 1;
    }
    return n * product(n - 1);
}
  1. A. 1
  2. B. 3
  3. C. 5
  4. D. 4

Question 7 of 10

Each recursive call has its own copy of extra. What value does bump(3) return?

public static int bump(int n)
{
    int extra = 2;
    if (n <= 0)
    {
        return extra;
    }
    return extra + bump(n - 1);
}
  1. A. 2
  2. B. 8
  3. C. 6
  4. D. 5

Question 8 of 10

A mirror chime prints both before and after the recursive call. What text does mirror(2) display?

public static void mirror(int n)
{
    if (n <= 0)
    {
        return;
    }
    System.out.print(n);
    mirror(n - 1);
    System.out.print(n);
}
  1. A. 21
  2. B. 12
  3. C. 2112
  4. D. 2211

Question 9 of 10

A tile layer uses this method to raise a base. What value does power(3, 4) return?

public static int power(int base, int exp)
{
    if (exp == 0)
    {
        return 1;
    }
    return base * power(base, exp - 1);
}
  1. A. 12
  2. B. 81
  3. C. 64
  4. D. 3

Question 10 of 10

Which statement about the method below is true?

public static int sumTo(int n)
{
    if (n == 0)
    {
        return 0;
    }
    return n + sumTo(n - 1);
}
  1. A. The call sumTo(0) never stops because there is no base case
  2. B. Each call uses the same n variable from the original caller
  3. C. sumTo(5) returns 15, and the same total can be produced with a loop that adds 5 through 1
  4. D. A recursive method cannot be replaced by iteration