How would the method analyzeThis behave when called on an instance of NumberAnalyzer containing an array initialized with {2, 5, 8, 11}?
Consider the following class definition and the attempt to compute the sum of even integers within an array using an enhanced for-loop.
public class NumberAnalyzer {
private int[] nums;
public int analyzeThis() {
int sum = 0;
for (int num : nums) {
if (num % 2 == 0) {
sum += num;
}
}
return sum;
}
}
How would the method analyzeThis behave when called on an instance of NumberAnalyzer containing an array initialized with {2, 5, 8, 11}?
The loop increments the sum by the array values that are divisible by 2: sum accumulates to 10.
The loop includes all numbers, even and odd, making the sum 26.
The loop exits prematurely and returns a sum of 2 given an array index out of bounds at the first non-even number.
The method returns a runtime error since the array includes values that are not integers.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | lsj08030922 | 1 | 1 | 0m 00s | 100 |
| #2 | hitrishabhatia | 1 | 2 | 0m 00s | 90 |
| #3 | suhanakochhar006 | 1 | 1 | 1m 26s | 14 |
| #4 | y.seong2027 | 2 | 3 | 3m 50s | -40 |
| #5 | gtsak31 | 1 | 1 | 3m 33s | -113 |
APFIVE