Multiple choice technology programming languages

What is the legal range of values for a variable declared as a byte?

  1. 0 to 256

  2. -128 to 127

  3. -127 to 128

  4. 0 to 255

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

A Java byte is a signed 8-bit integer. It can represent values from -128 to 127 (2^7 - 1), for a total of 256 distinct values (2^8).

AI explanation

In Java, a byte is an 8-bit signed two's-complement integer, giving it a range of -128 to 127 (one bit is used for the sign, and two's-complement representation allows one extra negative value compared to positive values). This asymmetry — 128 negative values but only 127 positive values plus zero — is a common trap in the other options that don't account for the sign bit correctly.