Analyzing Array Traversal Code
An integer array nums has been declared and initialized with one or more values. Which of the following code segments correctly counts the number of negative values in nums and stores the result in the counter variable?
I.
int counter = 0;
int i = -1;
while (i <= nums.length - 2)
{
i++;
if (nums[i] < 0)
{
counter++;
}
}
II.
int counter = 0;
for (int i = 1; i < nums.length; i++)
{
if (nums[i] < 0)
{
counter++;
}
}
III.
int counter = 0;
for (int i : nums)
{
if (nums[i] < 0)
{
counter++;
}
}
A
I only
B
I, II, and III
C
I and III only
D
II only
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | kaisuki | 1 | 1 | 0m 00s | 100 |
| #2 | deltasmpserver | 0 | 1 | 0m 00s | -10 |
| #3 | hmbin999 | 0 | 1 | 1m 03s | -73 |
| #4 | y.seong2027 | 1 | 3 | 10m 28s | -548 |
Items per page:
10
1 – 4 of 4
APFIVE