Java Enhanced For Loop Summation
Consider the following NumberAnalyzer class definition.
public class NumberAnalyzer {
private int[] nums;
public int analyzeThis() {
int sum = 0;
for (int num : nums) {
if (num % 2 == 0) {
sum += num;
}
}
return sum;
}
// Assume the constructor and other methods are defined elsewhere.
}
What is returned by a call to the analyzeThis method on a NumberAnalyzer object whose nums instance variable is initialized to {2, 5, 8, 11}?
A
The method returns a runtime error since the array includes values that are not integers.
B
The loop includes all numbers, even and odd, making the sum 26.
C
The loop increments the sum by the array values that are divisible by 2: sum accumulates to 10.
D
The loop exits prematurely and returns a sum of 2 given an array index out of bounds at the first non-even number.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | lsj08030922 | 1 | 1 | 0m 00s | 100 |
| #2 | hitrishabhatia | 1 | 2 | 0m 00s | 90 |
| #3 | Pelisx | 1 | 1 | 0m 36s | 64 |
| #4 | suhanakochhar006 | 1 | 1 | 1m 26s | 14 |
| #5 | y.seong2027 | 2 | 3 | 3m 50s | -40 |
| #6 | gtsak31 | 1 | 1 | 3m 33s | -113 |
| #7 | midnightasdark | 1 | 1 | 20m 44s | -1,144 |
Items per page:
10
1 – 7 of 7
APFIVE