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








Question

What is the output of the following code snippet?

public class Main{
   public static void main(String[] argv){
     3: boolean checked = true; 
     4: int result = 15, i = 10; 
     5: do { 
     6:   i--; 
     7:   if(i==8) checked = false; 
     8:   result -= 2; 
     9: } while(checked); 
     10: System.out.println(result); 
   }
}     
  1. 7
  2. 9
  3. 10
  4. 11
  5. 15
  6. The code will not compile because of line 8.




Answer



D.

Note

The code compiles without issue.

public class Main{
   public static void main(String[] argv){
      boolean checked = true; 
      int result = 15, i = 10; 
      do { /*from   www  . j a  va 2 s  . c om*/
        i--; 
        if(i==8) 
           checked = false; 
        result -= 2; 
      } while(checked); 
      System.out.println(result);    
   }
}