Tracing a Recursive Method
Consider the following class. What value is printed when the main method is executed?
public class WeirdRecursion {
public static int mystery(int n) {
if(n < 3)
return n;
return mystery(n-1) - mystery(n-2) + mystery(n-3);
}
public static void main(String[] args) {
System.out.println(mystery(5));
}
}
A
2
B
0
C
3
D
1
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | nabihamanahal | 2 | 2 | - | 200 |
| #2 | raufyildirim95 | 1 | 1 | 0m 46s | 54 |
| #3 | hiboyshello | 0 | 1 | - | -10 |
Items per page:
10
1 – 3 of 3
