Java OCA OCP Practice Question 1778

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 i = 0;
    for (   ; i<10; i++) ;      // (1)
    for (i=0;     ; i++) break; // (2)
    for (i=0; i<10;    ) i++;   // (3)
    for (   ;     ;    ) ;      // (4)
  }
}

Select the one correct answer.

  • (a) The code will fail to compile because the expression in the first section of the for statement (1) is empty.
  • (b) The code will fail to compile because the expression in the middle section of the for statement (2) is empty.
  • (c) The code will fail to compile because the expression in the last section of the for statement (3) is empty.
  • (d) The code will fail to compile because the for statement (4) is invalid.
  • (e) The code will compile without error, and the program will run and terminate without any output.
  • (f) The code will compile without error, but will never terminate when run.


(f)

Note

The code will compile without error, but will never terminate when run.

All the sections in the for header are optional and can be omitted (but not the semicolons).

An omitted loop condition is interpreted as being true.

Thus, a for loop with an omitted loop condition will never terminate, unless a break statement is encountered in the loop body.

The program will enter an infinite loop at (4).




PreviousNext

Related