OCA Java SE 8 Mock Exam 2 - OCA Mock Question 12
Question
Which statement will display each element of the array?
int myArray[] = {1,2,3,4,5};
- for(int n : myArray[]) { System.out.println(n); }
- for(int n : myArray) { System.out.println(n); }
- for(int n=1; n < 5; n++) { System.out.println(myArray[n]); }
- for(int n=1; n <= 4; n++) { System.out.println(myArray[n]); }
Answer
Note
You cannot use [] in a for-each statement.