Oracle
1Z0-850 · Question #46
You need an algorithm that must: Iterate through an array of primitive integers Print the value of each array element in index order If the value of the element is equal to 10, print the value of…
The correct answer is C. public static void foo(int[] list) { for(int i:list) { System.out.println(i); if (i==10) break; } }. See the full explanation below for the reasoning.
Question
You need an algorithm that must:
- Iterate through an array of primitive integers
- Print the value of each array element in index order
- If the value of the element is equal to 10, print the value of the element, and then terminate the iteration Which method correctly implements the algorithm?
Options
- Apublic static void foo(int[] list) { for(each(int in list)) { System.out.println(i); if (i==10) terminate; } }
- Bpublic static void foo(int[] list) { for(int i=0; i< list.length; i++) { System.out.println(i); if (i==10) continue; } }
- Cpublic static void foo(int[] list) { for(int i:list) { System.out.println(i); if (i==10) break; } }
- Dpublic static void foo(int[] list) { while(list.length > 0) { System.out.println(i); if (i==10) break; } }
- Epublic static void foo(int[] list) { for(int i=0; i < list.length; i++) { System.out.println(i); if (i==10) break; } }
How the community answered
(37 responses)- A14% (5)
- B3% (1)
- C78% (29)
- E5% (2)
Community Discussion
No community discussion yet for this question.