Multiple choice technology programming languages

Given: 11. int x = 3; 12. int y = 1; 13. if (x = y) { 14. System.out.println(“x = “ + x); 15. } What is the result?

  1. x = 1

  2. x = 3

  3. Compilation fails.

  4. An exception is thrown at runtime.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The code in line 13 uses assignment (=) instead of equality comparison (==). In Java, the if condition requires a boolean expression, but x = y results in an integer value, which cannot be converted to boolean. This causes a compilation error.