Multiple choice technology programming languages

Consider the following piece of code byte x = 0; x += 1;

  1. Results in x having the value 1.

  2. Causes a compiler error.

  3. Will require a cast (byte) before 1.

  4. Will give syntax error.

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

In Java, compound assignment operators such as += automatically cast the result of the operation back to the type of the left-hand variable. Therefore, x += 1 is evaluated as x = (byte)(x + 1). This compiles successfully without needing an explicit cast, resulting in x having the value 1.