Java OCA OCP Practice Question 3237

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 counter = 0;
    l1:/*from  ww w.j a va2 s.  c o m*/
    for (int i=0; i<10; i++) {
      l2:
      int j = 0;
      while (j++ < 10) {
        if (j > i) break l2;
        if (j == i) {
          counter++;
          continue l1;
        }
      }
    }
    System.out.println(counter);
  }
}

Select the one correct answer.

  • (a) The program will fail to compile.
  • (b) The program will not terminate normally.
  • (c) The program will write 10 to the standard output.
  • (d) The program will write 0 to the standard output.
  • (e) The program will write 9 to the standard output.


(a)

Note

The program will fail to compile since the label l2 cannot precede the declaration int j = 0.

For a label to be associated with a loop, it must immediately precede the loop construct.




PreviousNext

Related