Multiple choice technology databases

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

The compound assignment operator x += 1 is valid for byte type. The result of x += 1 is implicitly cast back to byte, so x will have the value 1. This is a special rule for compound assignment operators - they perform an implicit narrowing primitive conversion. No explicit cast is required, unlike simple assignment which would need (byte)1.