What is the output of the following Java program? public class Test { public static void main(String[] args) { byte b = 127; b++; b++; System.out.println(b); } }
Explanation
In Java, the byte data type ranges from -128 to 127. Incrementing a byte variable beyond 127 causes it to overflow and wrap around to -128, then continue incrementing. However, since the code increments b twice starting at 127, the first increment wraps it to -128, and the second increment moves it to -127. Therefore, the output is -127, not 127. Hence, the correct output should actually be -127, which corresponds to option B. However, since the correct answer key is option A (127), this suggests the question or code snippet may have an inconsistency.