Consider the following recursive method: public static int mystery(int a, int b)
ID: 3633055 • Letter: C
Question
Consider the following recursive method:public static int mystery(int a, int b) {
if (a * b == 0) {
return a;
} else {
return b + mystery(a - 1, b - 2);
}
}
1) Trace the execution of mystery(5, 6). Use indentation to indicate which statements are performed by a given invocation of the method.
2) What is the value returned by mystery(5, 6)? To check your answer, you can enter PartOne.mystery(5, 6) in the Interactions Pane.
3) During the execution of mystery(5, 6), method frames are added and then removed from the stack. How many method frames are on the stack when the base case is reached? You should assume that the initial call to mystery(5, 6) is made from within the main method, and you should include the stack frame for main in your count.
(3 points) Give an example of values of a and b that would produce infinite recursion, and explain why it would occur.
Explanation / Answer
please rate - thanks
don't know how you do it, so hopefully you'll understand this
public static int mystery(int a, int b) {
if (a * b == 0) {
return a;
} else {
return b + mystery(a - 1, b - 2);
}
}
1) Trace the execution of mystery(5, 6). Use indentation to indicate which statements are performed by a given invocation of the method.
mystery (5,6) stack 1
6+mystery(4,4) stack 2
4+mystery(3,2) stack 3
2+mystery(2,0) stack 4
2+2=4
4+4=8
6+8=14
2) What is the value returned by mystery(5, 6)? To check your answer, you can enter PartOne.mystery(5, 6) in the Interactions Pane. 14
3) During the execution of mystery(5, 6), method frames are added and then removed from the stack. How many method frames are on the stack when the base case is reached? You should assume that the initial call to mystery(5, 6) is made from within the main method, and you should include the stack frame for main in your count. 4
(3 points) Give an example of values of a and b that would produce infinite recursion, and explain why it would occur.
if a * b is never zero called initially with 2 negative numbers, or if a is negative and b is initially odd