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] + ” “);
}
β
Correct Answer: (C)
“1 3 5”
Explanation: 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”.