This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1A byte can be of what size?
Option A
In Java, byte is a primitive integer data type that always occupies 8 bits (1 byte). An 8-bit signed value uses one bit for the sign and the remaining bits for representing the magnitude. Therefore, a Java byte can represent 256 different values, ranging from -128 to 127.
The range can be calculated as: -27 to 27 - 1 = -128 to 127
Java Integer Data Types:
| Data Type | Size | Range |
|---|---|---|
| byte | 8 bits | -128 to 127 |
| short | 16 bits | -32,768 to 32,767 |
| int | 32 bits | -231 to 231 - 1 |
| long | 64 bits | -263 to 263 - 1 |
Q: 2What is the size of float and double in Java?
Option A
In Java, float and double are floating-point data types used to store numbers containing fractional values. The main difference between them is their size and precision.
A float occupies 32 bits (4 bytes), while a double occupies 64 bits (8 bytes). Because double has twice as many bits as float, it generally provides greater precision and a larger range of values.
class Example
{
public static void main(String args[])
{
float a = 11.5f;
double b = 22.75;
System.out.println("Float: " +a);
System.out.println("Double: " +b);
}
}
OUTPUT
Float: 11.5
Double: 22.75
| Category | Data Types | Size | Default Value | Description |
|---|---|---|---|---|
| Integer | byte | 8 bits | 0 | Stores small integer values from -128 to 127. |
| short | 16 bits | 0 | Stores integer values from -32,768 to 32,767. | |
| int | 32 bits | 0 | Commonly used for storing integer values. | |
| long | 64 bits | 0L | Stores large integer values. | |
| Floating-Point | float | 32 bits | 0.0f | Stores single-precision decimal values. |
| double | 64 bits | 0.0d | Stores double-precision decimal values. | |
| Character | char | 16 bits | '\u0000' | Stores a single Unicode character. |
| Boolean | boolean | Not Specified | false | Stores either true or false. |
Q: 3What will happen when you compile and run the following code?
public class MyClass
{
static int i;
public static void main(String argv[])
{
System.out.println(i);
}
}
Option D
In Java, variables declared as class members (fields), including static variables, receive default values automatically if they are not explicitly initialized.
The default value of an int variable is 0. Therefore, the variable i in the given program is automatically initialized to 0, and the program prints 0.
Default Values of Java Fields:
| Data Type | Default Value |
|---|---|
| byte, short, int, long | 0 |
| float | 0.0f |
| double | 0.0d |
| char | '\u0000' |
| boolean | false |
| Reference Types | null |
Q: 4What happens in the following code?
final int x;
x = 20;
System.out.println(x);
Option B
In Java, a final variable can be assigned a value once, but it does not necessarily have to be initialized at the time of declaration. When a final local variable is declared without an initial value and is assigned a value later before its first use, it is called a blank final variable. Therefore, the following code is completely valid.
The important rule is that a final variable can be assigned only once. In the given code, x is declared first and then assigned 20. Since x has not been assigned any value before, this is its first and only assignment, so the compiler accepts the code.
Also, a local final variable does not automatically receive a default value such as 0. It must be assigned before it is used.
E.g.: 1
public class FinalVariableDemo
{
public static void main(String args[])
{
final int x; // Declared but not initialized
x=20; // Assigned for the first and only time
}
}
E.g.: 2
public class FinalVariableDemo
{
public static void main(String args[])
{
final int x;
x=20;
x=30; // Compilation Error
}
}
Q: 5What is the output?
byte b=10;
b=(byte)(b+200);
System.out.println(b);
Option C
In Java, the byte data type is a signed 8-bit integer. Therefore, it can store values only in the range -128 to 127. In the given statement, b is initially assigned the value 10.
In statement b=(byte)(b+200);, an important Java rule is involved, when arithmetic is performed on a byte, the value is automatically promoted to int. Therefore, b+200 is calculated as an int, i.e., 10+200 = 210.
At this stage, the result is 210, which is an int. Since 210 cannot be directly stored in a byte, the explicit cast (byte) is used.
However, 210 is outside the valid byte range of -128 to 127. When an int value is explicitly converted to a byte, Java retains the lowest 8 bits of the value. The resulting 8-bit pattern is interpreted as a signed two's-complement value.
(210)2 = 00000000 00000000 00000000 11010010
The lowest 8 bits: 11010010
Because the leftmost bit is 1, this represents a negative number in signed 8-bit two's-complement representation. Its decimal value is 210-256 = -46.
Therefore, the value stored in b becomes -46, and System.out.println(b); prints -46.
Note:
If the explicit cast (byte) is not present, the statement produces a Compilation Error because Java promotes the byte value to int during arithmetic. Therefore, b+200 has the type int, and Java does not allow an int value to be assigned directly to a byte variable without an explicit narrowing conversion.
E.g.:
class MyDriver
{
public static void main(String[] args)
{
byte b=10;
b=(b+200);
System.out.println(b);
}
}
When we compile the code without the explicit (byte) cast, the compiler generates an error such as “incompatible types: possible lossy conversion from int to byte” at the statement b=(b+200);.
Q: 6Which of the following lines will compile without warning or error?
Option D
In Java, a variable declaration must follow the rules of the specified data type. Numeric literals also have default data types in Java, so assigning a literal to a variable is allowed only when the literal is compatible with the variable type.
Option (A): float f = 1.3; This statement does not compile.
In Java, a decimal floating-point literal such as 1.3 is treated as a double literal by default. A double value cannot be assigned directly to a float variable because double has greater precision and range than float.
Option (B): char c = "a"; This statement does not compile.
A char variable stores a single character, and character literals in Java must be enclosed in single quotation marks.
Option (C): byte b = 257; This statement does not compile.
A byte in Java can store values only from -128 to 127.
Option (D): int i = 10; This statement compiles without warning or error.
An integer literal such as 10 is treated as an int by default, and the variable i is also declared as int.
Q: 7Which one of the following is not a primitive data type in Java?
Option A
Java has eight primitive data types, byte, short, int, long, float, double, char, and boolean. Primitive types represent basic values and are not objects.
In contrast, classes are reference types, not primitive data types. A class is a blueprint used to create objects. Therefore, Classes is not a primitive data type.
Q: 8Which declaration is valid?
Option C
In Java, numeric literals have specific default data types. A decimal floating-point literal such as 10.5 is treated as a double by default. If we want a decimal literal to be treated as a float, we use the suffix f or F, as in 10.5f. Java allows a float value to be automatically converted to double because double has a larger range and greater precision. Therefore, the declaration double d = 10.5f; is valid.
Option (A): float f = 10.5; [Invalid]
Here, 10.5 is a double literal by default. The variable f is of type float. Java does not automatically convert a double into a float because this conversion can result in loss of precision.
E.g.:
Option (B): long l = 2147483648; [Invalid]
In Java, an integer literal without a suffix such as L is normally treated as an int if its value can be represented by the int data type. The maximum value of an int is 2,147,483,647, whereas 2147483648 is one greater than the maximum int value.
Therefore, the compiler cannot treat 2147483648 as an int literal, and the statement produces a Compilation Error. Although a long variable can store this value, the literal itself must be explicitly written as a long literal using the L suffix.
E.g.:
Here, L tells the compiler that 2147483648 is a long literal, so it can be assigned to the long variable.
Option (D): byte b = 128; [Invalid]
In Java, the byte data type is a signed 8-bit integer and can store values only from -128 to 127. The value 128 is outside this range, so it cannot be directly assigned to a byte variable. Therefore, the following declaration produces a Compilation Error.
E.g.:
The statement byte b = (byte)128; is valid and code will compile, but the value becomes -128 because the conversion from int to byte retains the lower 8 bits of the value and interprets them using signed 2’s-complement representation.
Q: 9Number of primitive data types in Java are:
Option D
Java provides eight primitive data types. Primitive data types are the basic data types built into the Java language. They are used to store simple values directly rather than references to objects.
The eight primitive data types are byte, short, int, long, float, double, char, and boolean.
Q: 10What is the default value of a char variable in Java?
Option A
In Java, the char data type is used to store a single Unicode character. For a char variable that is an instance variable, static variable, or array element, Java automatically assigns the default value '\u0000', which is called the null character.
It is important not to confuse the null character '\u0000' with the character '0'. The character '0' is a printable character and has the Unicode value 48, whereas '\u0000' has the numeric value 0.
E.g.:
class Example
{
char ch;
public static void main(String args[])
{
Example obj = new Example();
System.out.println((int)obj.ch);
System.out.println(obj.ch == '\u0000');
}
}
OUTPUT
0
True
| Value | Meaning |
|---|---|
| '\u0000' | Null character, default value of char |
| 0 | Numeric value of '\u0000' |
| '0' | Character zero, whose Unicode value is 48 |
| ' ' | Space character, whose Unicode value is 32 |
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.