Enhanced For-Loop Array Summation
Consider the following NumberAnalyzer class.
public class NumberAnalyzer {
private int[] nums;
public int analyzeThis() {
int sum = 0;
for (int num : nums) {
if (num % 2 == 0) {
sum += num;
}
}
return sum;
}
}
What value is returned by a call to the analyzeThis method if the nums instance variable is initialized with the array {2, 5, 8, 11}?
A
The loop increments the sum by the array values that are divisible by 2: sum accumulates to 10.
B
The method returns a runtime error since the array includes values that are not integers.
C
The loop exits prematurely and returns a sum of 2 given an array index out of bounds at the first non-even number.
D
The loop includes all numbers, even and odd, making the sum 26.
Question Leaderboard
Not enough data yet to show leaderboard.
