What is the maximum value that can be stored in a byte variable in Java?
A. 127
B. 255
C. 32767
D. 64
Answer: Option A
Solution (By Examveda Team)
In Java, the byte data type is a signed 8-bit integer type. Being signed means that it can represent both positive and negative values. The maximum value that can be stored in a byte variable is 127.The reason is that a byte variable uses 8 bits to represent numbers, and one of those bits is used to represent the sign (positive or negative). Therefore, the remaining 7 bits are available to represent the magnitude of the number.
In binary, with 7 bits, you can represent numbers from 0 to 127. The highest bit (leftmost bit) is used for sign, where 0 represents positive and 1 represents negative.
So, the maximum value for a byte variable in Java is 2^7 - 1, which equals 127.
Therefore, the correct answer is Option A: 127.
Join The Discussion
Comments (2)
Related Questions on Data Types and Variables
Which of the following is not a valid identifier for a Java variable?
A. my_var
B. _myVar
C. 3rdVar
D. $var

in the question, there wasn't mention about signed or unsigned so the maximum value should be 255
correct ans is A. Byte range is -128 to +127