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}; 
  1. for(int n : myArray[]) { System.out.println(n); }
  2. for(int n : myArray) { System.out.println(n); }
  3. for(int n=1; n < 5; n++) { System.out.println(myArray[n]); }
  4. for(int n=1; n <= 4; n++) { System.out.println(myArray[n]); }




Answer



B, C, and D

Note

You cannot use [] in a for-each statement.