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








Question

What is the result of the following code snippet?

     3: int m = 9, n = 1, x = 0; 
     4: while(m > n) { 
     5:   m--; 
     6:   n += 2; 
     7:   x += m + n; 
     8: } 
     9: System.out.println(x); 
  1. 1
  2. 2
  3. 3
  4. 6
  5. 7
  6. The code will not compile because of line 7.




Answer



D.

Note

public class Main{
   public static void main(String[] argv){
        int m = 4, n = 1, x = 0; 
        while(m > n) { 
          m--; //from  w w  w. j a  v  a2  s .c o m
          n += 2; 
          x += m + n; 
        } 
        System.out.println(x);    
   }
}