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








Question

What is the output of the following code snippet?

public class Main{
   public static void main(String[] argv){
         3: int c = 2; 
         4: int result = 4; 
         5: result += ++c; 
         6: System.out.println(result); 
   
   }
}
  1. 8
  2. 6
  3. 7
  4. 9
  5. 10
  6. The code will not compile because of line 5.




Answer



C.

Note

The code compiles successfully.

public class Main{
   public static void main(String[] argv){
         int c = 2; 
         int result = 4; 
         result += ++c; 
         System.out.println(result); 
   
   }
}

The code above generates the following result.