asked 49.1k views
1 vote
Which of the following statement displays the list elements correctly?

int list[ ] = {11,22,33,44,55,66,77,88,99};
(A) for (int k=0; list item; k++)
System.out.print(item + " ");
(B) for (int item: list)
System.out.print(item + " ");
(C) for (int k=0; int item; k++)
System.out.print(item + " ");
(D) for (int k=0; list item; k++)
System.out.print(item[k] + " ");

1 Answer

5 votes

Final answer:

The statement that correctly displays the list elements from the given array is option (B), using an enhanced for loop in Java.

Step-by-step explanation:

The correct statement to display the list elements from the array int list[] = {11,22,33,44,55,66,77,88,99} is option (B). This uses the enhanced for loop, which iterates through each element of the array and stores it in the variable item. The correct syntax for the enhanced for loop in Java is:

for (int item : list) {
System.out.print(item + " ");
}

Options (A), (C), and (D) contain either incorrect syntax or would not compile in Java, thus they do not correctly display the list elements.

answered
User Stellan Lindell
by
7.3k points

No related questions found