Question: Regarding the JAVA language assume that int[ ] a = {3, 5, 2, 4, 7, 3, 4, 5, 3} and consider the two recursive methods
Regarding the JAVA language assume that int[ ] a = {3, 5, 2, 4, 7, 3, 4, 5, 3} and consider the two recursive methods below foo and bar. Consider these to answer the following questions.
1. What is the result of calling foo(a, 3, 0);?
2. What is the result of calling foo(a, 3, 4);?
3. What is the result of calling bar(a, 5);?
4. What is the result of calling bar(a, 9);?
public int foo(int[ ] a, int b, int j) {
if (j < a.length)
if (a[j] != b)
return foo (a, b, j+1);
else
return foo (a, b, j+1) + 1;
else
return 0;
}
public int bar(int[ ] a, int j) {
if (j < a.length)
return a[j] + bar(a, j+1);
else
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
