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
- Match List I (programming terms) with List II (meaning). List I: A. JNDI, B. RMI, C. JDK, D. JRE. List II: I. Runtime support for running J…
- Match List I (programming paradigms) with List II (characteristics). List I: A. Imperative, B. Object-oriented, C. Logic, D. Functional. Li…
- Suppose you have eight black-and-white images taken with a 1-megapixel camera and one 8-colour image taken with an 8-megapixel camera. How…
- Which of the statements given below are correct? The midpoint (or Bresenham) algorithm for rasterizing lines is optimized relative to DDA a…
- What is the transformation matrix that transforms the square with vertices (1, 1), (−1, 1), (−1, −1), (1, −1) into the parallelogram with c…
- A Bezier curve P(t) is defined by four control points P0 = (−2, 0), P1 = (−2, 4), P2 = (2, 4), P3 = (2, 0). Which statements are correct? A…