Complete the code to return the first even number found in the array.
public int findFirstEven(int[] numbers) {
for (int num : numbers) {
if (num % 2 == 0) {
return [1];
}
}
return -1;
}The method returns the first even number found in the array, which is the variable num inside the loop.
Complete the code to return true if any number in the array is negative.
public boolean hasNegative(int[] numbers) {
for (int num : numbers) {
if (num < 0) {
return [1];
}
}
return false;
}false inside the loop.The method returns true immediately when it finds a negative number.
Fix the error in the code to return the index of the first zero in the array, or -1 if none found.
public int findFirstZero(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
return [1];
}
}
return -1;
}arr[i] instead of the index.The method should return the index i where the zero is found, not the value at that index.
Fill both blanks to return the sum of positive numbers in the array.
public int sumPositive(int[] nums) {
int sum = 0;
for (int [1] = 0; [2] < nums.length; i++) {
if (nums[i] > 0) {
sum += nums[i];
}
}
return sum;
}The loop variable i is used for initialization and condition to iterate over the array.
Fill all three blanks to return true if all numbers in the array are positive.
public boolean allPositive(int[] arr) {
for (int [1] = 0; [2] < arr.length; [3]++) {
if (arr[i] <= 0) {
return false;
}
}
return true;
}The loop variable i is used consistently for initialization, condition, and increment.