public static boolean isTrue(int n){
if(n%2 == 0)
return true;
else
return false;
}
public static int Method(int[] numbers, int startIndex) {
if(startIndex >= numbers.length)
return 0;
if (isTrue(numbers[startIndex]))
return 1 + Method(numbers, startIndex + 1);
else
return Method(numbers, startIndex + 1);
}
What is the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 2, 3, 3, 4, 4, 4, 5, 5}, startIndex = 0
Answer:
//TestCode.java public class TestCode { public static boolean isTrue(int n){ if(n%2 == 0) return true; else return false; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length) return 0; if (isTrue(numbers[startIndex])) return 1 + Method(numbers, startIndex + 1); else return Method(numbers, startIndex + 1); } public static void main(String[] args) { int[] numbers = {1, 2, 2, 3, 3, 4, 4, 4, 5, 5}; int startIndex = 0; System.out.println(Method(numbers,startIndex)); } }
5
Get Answers For Free
Most questions answered within 1 hours.