In: Computer Science
Consider the following two methods:
public static boolean isTrue(int n){
if (n <= 1)
return false;
for (int i = 2; i < n; i++){
if (n % i == 0)
return false;
}
return true;
}
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, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13}, startIndex = 0
// TestCode.java
public class TestCode {
public static boolean isTrue(int n){
if (n <= 1)
return false;
for (int i = 2; i < n; i++){
if (n % i == 0)
return false;
}
return true;
}
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, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
int startIndex = 0;
System.out.println(Method(numbers,startIndex));
}
}

6

the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13}, startIndex = 0 is 6