All mock tests are FREE till 31 December — register to save your progress

Consider the following recursive Java function:

public static String F(int N) {
  if (N <= 0) return "-";
  return F(N - 3) + N + F(N - 2) + N;
}

The value of F(5) is:

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: D. -2-25-3-1-135

Explanation

F(1) = "-1-1". F(2) = F(−1) + 2 + F(0) + 2 = "-2-2". F(3) = F(0) + 3 + F(1) + 3 = "-3-1-13". F(5) = F(2) + 5 + F(3) + 5 = "-2-2" + "5" + "-3-1-13" + "5" = -2-25-3-1-135.

More Programming Languages and Computer Graphics questions