Java OCA OCP Practice Question 1864

Question

What will be the result of attempting to compile and run the following program?

public class Main{
   public static void main (String args []){
      int x  = 0;
      labelA :   for  (int i=10; i<0; i--){
         int j = 0;
         labelB ://from   w w w. ja  va  2 s . c  o m
         while  (j < 10){
            if  (j > i) break labelB;
            if  (i == j){
               x++;
               continue labelA;
             }
            j++;
          }
         x--;
       }
      System.out.println (x);
    }
}

Select 1 option

  • A. It will not compile.
  • B. It will go in infinite loop when run.
  • C. The program will write 10 to the standard output.
  • D. The program will write 0 to the standard output. E. None of the above.


Correct Option is  : D

Note

This is just a simple code that is meant to confuse you.

Notice the for statement: for (int i=10; i<0; i--).

i is being initialized to 10 and the test is i<0, which is false.

Therefore, the control will never get inside the for loop, none of the weird code will be executed, and x will remain 0, which is what is printed.




PreviousNext

Related