Recursive Method with a Static Counter
What is printed when the following program is executed?
public class FactorialCounter {
static int counter = 0;
public static int factorial(int n) {
counter++;
if(n <= 1) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
int result = factorial(4);
System.out.println(result + " " + counter);
}
}
A
24 5
B
24 4
C
120 5
D
120 4
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE