Java OCA OCP Practice Question 1788

Question

Given the following code, which statement is true?.

public class Main {
  public static void main(String[] args) {
    int k=0;//from  ww  w .j a  v  a2 s . co  m
    int l=0;
    for (int i=0; i <= 3; i++) {
      k++;
      if (i == 2) break;
      l++;
    }
    System.out.println(k + ", " + l);
  }
}

Select the one correct answer.

  • (a) The program will fail to compile.
  • (b) The program will print 3, 3, when run.
  • (c) The program will print 4, 3, when run, if break is replaced by continue.
  • (d) The program will fail to compile if break is replaced by return.
  • (e) The program will fail to compile if break is by an empty statement.


(c)

Note

As it stands, the program will compile correctly and will print "3, 2" when run.

If the break statement is replaced with a continue statement, the loop will perform all four iterations and will print "4, 3".

If the break statement is replaced with a return statement, the whole method will end when i equals 2, before anything is printed.

If the break statement is simply removed, leaving the empty statement ( ;), the loop will complete all four iterations and will print "4, 4".




PreviousNext

Related