P

Pushkar • 380 Points
Valuable

Q. What is the output of the following code?

Code:
int[] nums = {1, 2, 3, 4, 5};
for (int i = 0; i < nums.length; i += 2) {
System.out.print(nums[i] + ” “);
}
  • (A) “1 2 3 4 5”
  • (B) “2 4”
  • (C) “1 3 5”
  • (D) “5 3 1”
  • Correct Answer - Option(C)
  • Views: 109
  • Filed under category JAVA

Explanation by: Pushkar
This for loop iterates through the elements of the nums array, using the index variable i, but only printing out the elements with even indexes. The System.out.print statement prints each element followed by a space, resulting in the output “1 3 5”.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.


Question analytics