Java · Question 22 · Medium

What will be the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        int x = 5;
        x += x++ * 2;
        System.out.println(x);
    }
}
  1. 11
  2. 16
  3. 17
  4. 15
Answer and explanation

Answer: D. 15

Explanation: x += e works like x = x + e, and Java evaluates the left x first, giving 5. Then x++ gives 5 (x briefly becomes 6) and 5 * 2 = 10. The sum 15 is assigned to x, replacing the 6.

Previous: Which of the following statements about interfaces is true in Java 8 and later? · Next: What will be the output of the following Java program?

Practise java

Try a fresh set of java questions with an explanation after every answer, free and without signing up: Java practice. Or sit a full placement mock test.

More java questions

More technical & programming practice

Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge