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); } }

Java concepts and its programming MCQs for PPSC, FPSC, NTS, and Pakistan government job tests. Select an option below, then read the explanation.

PPSCFPSCNTSPakistan govt jobs
Subject
Java concepts and its programmingsoftware-engineering-mcqs › java-concepts-and-its-programming
Published
18 Apr 2019
Last updated
28 May 2026

Browse all Java concepts and its programming MCQs

Choose the correct answer

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.

PakQuizHub — free MCQs and past papers for Pakistan government job tests. Content is for educational practice only.