Vidyalelo
Java Programming · Q9

Data Types and Variables

Programming · Java Programming · question 9

Q9

What is the range of values that can be stored in a short variable in Java?

A.
-128 to 127
B.
-32,768 to 32,767
Answer
C.
0 to 65,535
D.
-1,000,000 to 1,000,000

Answer: Option B

Solution

Answer: Option B
Solution:
The correct answer is Option B: -32,768 to 32,767.

In Java, a short variable is a 16-bit signed integer type, which means it can store values in the range of -32,768 to 32,767.

Here's the breakdown:

Option A: -128 to 127 - This range corresponds to a byte data type, not a short.

Option B: -32,768 to 32,767 - This is the correct range for a short variable in Java.

Option C: 0 to 65,535 - This range corresponds to an unsigned short (also known as char) in Java. A regular short is signed and can have both positive and negative values.

Option D: -1,000,000 to 1,000,000 - This range exceeds the capacity of a short variable, which can only store values up to 32,767.

So, the range of values that can be stored in a short variable in Java is -32,768 to 32,767.