Recursion MCQ Practice

Question 1 of 10

A garden plot helper adds consecutive row numbers. What value does sumTo(5) return?

public static int sumTo(int n)
{
    if (n == 0)
    {
        return 0;
    }
    return n + sumTo(n - 1);
}
  1. A. 5
  2. B. 10
  3. C. 15
  4. D. 120

Question 2 of 10

A lighthouse blinks every other count. What text does skip(7) display?

public static void skip(int n)
{
    if (n <= 0)
    {
        return;
    }
    System.out.print(n);
    skip(n - 2);
}
  1. A. 7654321
  2. B. 7531
  3. C. 1357
  4. D. 7

Question 3 of 10

A banner counter looks for the letter A. What value does countA("BANANA") return?

public static int countA(String s)
{
    if (s.length() == 0)
    {
        return 0;
    }
    int add = 0;
    if (s.substring(0, 1).equals("A"))
    {
        add = 1;
    }
    return add + countA(s.substring(1));
}
  1. A. 6
  2. B. 0
  3. C. 1
  4. D. 3

Question 4 of 10

A canoe club flips the tag "no" with reverse. What value does reverse("no") return?

public static String reverse(String s)
{
    if (s.length() <= 1)
    {
        return s;
    }
    return reverse(s.substring(1)) + s.substring(0, 1);
}
  1. A. on
  2. B. no
  3. C. n
  4. D. ono

Question 5 of 10

A clerk prints a leftover digit before the recursive call. What text does showDigits(472) display?

public static void showDigits(int n)
{
    if (n < 10)
    {
        System.out.print(n);
        return;
    }
    System.out.print(n % 10);
    showDigits(n / 10);
}
  1. A. 472
  2. B. 274
  3. C. 2
  4. D. 742

Question 6 of 10

A rope-cut helper reduces two lengths. What value does reduce(48, 18) return?

public static int reduce(int a, int b)
{
    if (b == 0)
    {
        return a;
    }
    return reduce(b, a % b);
}
  1. A. 48
  2. B. 18
  3. C. 6
  4. D. 0

Question 7 of 10

How many times is allSame called in total during allSame("aaab"), including the original call?

public static boolean allSame(String s)
{
    if (s.length() <= 1)
    {
        return true;
    }
    if (!s.substring(0, 1).equals(s.substring(1, 2)))
    {
        return false;
    }
    return allSame(s.substring(1));
}
  1. A. 1
  2. B. 4
  3. C. 3
  4. D. 2

Question 8 of 10

A crate stamp uses maxDigit to keep the largest digit. What value does maxDigit(3192) return?

public static int maxDigit(int n)
{
    if (n < 10)
    {
        return n;
    }
    int rest = maxDigit(n / 10);
    int d = n % 10;
    if (d > rest)
    {
        return d;
    }
    return rest;
}
  1. A. 3
  2. B. 2
  3. C. 3192
  4. D. 9

Question 9 of 10

A booth helper prints a label before and a tag after each recursive call. What text does mark(2) display?

public static void mark(int n)
{
    if (n <= 0)
    {
        return;
    }
    System.out.print("A" + n);
    mark(n - 1);
    System.out.print("B" + n);
}
  1. A. A2A1B1B2
  2. B. A2B2A1B1
  3. C. A1A2B2B1
  4. D. B2B1A1A2

Question 10 of 10

Which statement about recursive methods in this course is true?

public static int product(int n)
{
    if (n <= 1)
    {
        return 1;
    }
    return n * product(n - 1);
}
  1. A. Students must write a new recursive method to replace product
  2. B. product(4) returns 24, each call has its own n, and a loop multiplying 4 down to 1 can match that result
  3. C. The parameter n is shared by every call, so changing n in product(1) changes product(4)
  4. D. Removing the n <= 1 return still lets the method halt after four multiplications