OCA Java SE 8 Mock Exam - OCA Mock Question 1








Question

What is the output of the following program?

     1: public class Main { 
     2:  public static void main(String[] args) { 
     3:    int x = 5, j = 0; 
     4:    for(int i=0; i<3; )  
     5:      INNER: do { 
     6:        i++; x++; 
     7:        if(x > 10) break INNER; 
     8:        x += 4; 
     9:        j++; 
     10:      } while(j <= 2); 
     11:    System.out.println(x); 
     12: } 
     13:} 
  1. 10
  2. 12
  3. 13
  4. 17
  5. The code will not compile because of line 4.
  6. The code will not compile because of line 6.




Answer



B.

Note

The code compiles and runs without issue; therefore, E and F are incorrect.

The following code adds println to the statements and shows the result of code execution.

public class Main {
  public static void main(String[] args) {
    int x = 5, j = 0;
    for (int i = 0; i < 3;) {
      System.out.println("for x:"+x);
      System.out.println("for j:"+j);
      System.out.println("for i:"+i);
      INNER: do {
//from  w  ww  .  ja va2s .  com
        System.out.println("while x:"+x);
        System.out.println("while j:"+j);
        System.out.println("while i:"+i);

        i++;
        x++;
        if (x > 10)
          break INNER;
        x += 4;
        j++;
      } while (j <= 2);
    }
    System.out.println(x);
  }
}

The code above generates the following result.