Question: Consider the following instance variable and method. private int[] nums; /** print the elements that are in the odd index locations off an array: EX:
Consider the following instance variable and method.
private int[] nums;
/** print the elements that are in the odd index locations off an array: EX: 1, 3, 5, etc. */
public void printOddIndices()
{
/* missing code */
}
Which of the following replacements for /* missing code */ correctly implements the method printOddIndices()?
-
for (int x : nums)
{
if (nums[x] % 2 == 1)
{
System.out.println(x);
}
}
-
for (int x : nums)
{ i
f (x % 2 == 1)
{ System.out.println(x);
}
}
-
for (int k = 0; k < nums.length; k++)
{ if (k % 2 == 0)
{
System.out.println(nums[k]);
}
}
-
for (int k = 0; k < nums.length; k++)
{ if (k % 2 == 1)
{
System.out.println(nums[k]);
}
}
-
for (int k = 0; k < nums.length; k++)
{ if (nums[k] % 2 == 1)
{
System.out.println(k);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
