Java OCA OCP Practice Question 236

Question

Given:

3. public class Main {
4.   public static void main(String[] args) {
5.     int[] ia = {1,3,5,7,9};
6.     for(int x : ia) {
7.       for(int j = 0; j < 3; j++) {
8.         if(x > 4 && x < 8) continue;
9.         System.out.print(" " + x);
10.         if(j == 1) break;
11.         continue;
12.       }//w ww .j  av  a 2 s  .  co  m
13.       continue;
14.     }
15.   }
16. }

What is the result?

  • A. 1 3 9
  • B. 5 5 7 7
  • C. 1 3 3 9 9
  • D. 1 1 3 3 9 9
  • E. 1 1 1 3 3 3 9 9 9
  • F. Compilation fails


D is correct.

Note

For unlabeled continue statements, the current iteration stops early and execution jumps to the next iteration.

The last two continue statements are redundant!




PreviousNext

Related