OCA Java SE 8 Operators/Statements - OCA Mock Question Operator and Statement 17








Question

How many times will the following code print "Hello World"?

     3: for(int i=0; i<10 ; ) { 
     4:   i = i++; 
     5:   System.out.println("Hello World"); 
     6: } 
     
  1. 9
  2. 10
  3. 11
  4. The code will not compile because of line 3.
  5. The code will not compile because of line 5.
  6. The code contains an infinite loop and does not terminate.




Answer



F.

Note

The optional update statement of the for loop is missing.

The expression (i = i++;) inside the loop increments i but then assigns i the old value. i stays in value 0 for ever.

The loop will repeat infinitely.