Java OCA OCP Practice Question 3235

Question

Which statements are true regarding the execution of the following code?

public class Main {
  public static void main(String[] args) {
    int j = 5;//from  ww w  . j  av  a  2  s.com

    for (int i = 0; i< j; i++) {
      assert i < j-- : i > 0;
      System.out.println(i * j);
    }
  }
}

Select the two correct answers.

  • (a) An AssertionError will be thrown if assertions are enabled at runtime.
  • (b) The last number printed is 4, if assertions are disabled at runtime.
  • (c) The last number printed is 20, if assertions are disabled at runtime.
  • (d) The last number printed is 4, if assertions are enabled at runtime.
  • (e) The last number printed is 20, if assertions are enabled at runtime.


(c) and (d)

Note

The variable j is only decremented if assertions are enabled.

The assertion never fails, since the loop condition ensures that the loop body is only executed as long as i<j is true.

With assertions enabled, each iteration decrements j by 1.

The last number printed is 20 if assertions are disabled at runtime, and the last number printed is 4 if assertions are enabled at runtime.




PreviousNext

Related