Java Array Iteration and Filtering
Assume that the array arr has been defined and initialized as follows.
int[] arr = /* initial values for the array */ ;
Which of the following code segments correctly prints only the odd integers contained in arr?
A
for (int x : arr)
{
if (x % 2 == 1)
{
System.out.println(arr[x]);
}
}
B
for (int k = 1; k < arr.length; k++)
{
if (arr[k] % 2 == 1)
{
System.out.println(arr[k]);
}
}
C
for (int x : arr)
{
if (x % 2 == 1)
{
System.out.println(x);
}
}
D
for (int x : arr)
{
if (arr[x] % 2 == 1)
{
System.out.println(arr[x]);
}
}
Question Leaderboard
Not enough data yet to show leaderboard.
