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








Question

What is the output of the following code?

     1: public class Main { 
     2:   public static void main(String[] args) { 
     3:     int x = 5 * 8 % 3; 
     4:     System.out.println(x); 
     5: }
     6:} 
  1. 1
  2. 3
  3. 5
  4. 6
  5. The code will not compile because of line 3.




Answer



A.

Note

The * and % have the same operator precedence.

The output is 1 and A is the correct answer.

public class Main { 
  public static void main(String[] args) { 
     int x = 5 * 8 % 3; 
     System.out.println(x); 
  }
}