I have some practice questions for Java here. We are supposed to determine the answer without using a compiler.
Refer to the following method:
public static int product(int n){
if (n <= 1)
return 1;
else
return n * product(n-2);
}
What is the output when product(6) is called?
A) 1
B) 8
C) 12
D) 48
E) 70
According to the answers, the correct output is 48. I don't really understand why this is true. 6 doesn't meet the base case, so it goes to the else statement. So 6, then product(6-2) = product(4), which goes to product(2), which goes to product(0) so that produces 6 * 4, 4 * 2, 2 * 0, 0 * 0. But that's 32, not 48? Is there something I'm missing?
product(25) returns -1181211311 for some reason, and I'm not really sure why this happens either. Is it because there's a stack overflow in the recursive calls or something?
Explanations would be extremely helpful, thank you!