OCA Java SE 8 Mock Exam Review - OCA Mock Question 26








Question

What is the output of the following code?

public class Main { 
    public static void main(String[] args) { 
        int a = 10; 
        long b = 20; 
        short c = 30; 
        System.out.println(++a + b++ * c); 
    } 
} 
  1. 611
  2. 641
  3. 930
  4. 960




Answer



A

Note

The prefix increment operator ++ used with the variable a will increment its value before it is used in the expression ++a + b++ * c.

The postfix increment operator ++ used with the variable b will increment its value after its initial value is used in the expression ++a + b++ * c.

Therefore, the expression ++a + b++ * c, evaluates with the following values:

11 + 20 * 30 
public class Main { 
    public static void main(String[] args) { 
        int a = 10; 
        long b = 20; 
        short c = 30; 
        System.out.println(++a + b++ * c); 
    } 
} 

The code above generates the following result.