Java OCA OCP Practice Question 1774

Question

What will be the output when running the following program?.

public class Main {
 public static void main(String[] args) {
   int i=0;
   int j;
   for (j=0; j<10; ++j) { i++; }
   System.out.println(i + " " + j);
 }
}

Select the two correct answers.

  • (a) The first number printed will be 9.
  • (b) The first number printed will be 10.
  • (c) The first number printed will be 11.
  • (d) The second number printed will be 9.
  • (e) The second number printed will be 10.
  • (f) The second number printed will be 11.


(b) and (e)

Note

Both the first and the second number printed will be 10.

Both the loop body and the increment expression will be executed exactly 10 times.

Each execution of the loop body will be directly followed by an execution of the increment expression.

Afterwards, the condition j<10 is evaluated to see whether the loop body should be executed again.




PreviousNext

Related