Assume that you have an array of integers named arr. Which of these code segments print the same results?

int i = 0;
while (i < arr.length)
{
System.out.println(arr[i]);
i++;
}
int i;
for (i = 0; i <= arr.length; i++)
{
System.out.println(arr[i]);
}
for (int i : arr)
{
System.out.println(i);
}
I and II only
II and III only
I and III only
All three print the same results
All three print different results

Respuesta :

Answer:

II and III only

Explanation:

In Code segment II, the output of the array will be started form arr[0] and ends at the arr[length]. Because loop starts from 0 and ends at length of array. This will print the full array data.

In code segment III, the output will be all values of array as loop starts form first index and ends at last index.

On the other hand I code segment prints all array values except last value of the array. As the loop shows that, it will terminate at second last value of the array.

ACCESS MORE
EDU ACCESS