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: 1What is the result of expression 5.45+”3.2”?
Option D
In Java, the + operator is used for both numeric addition and string concatenation. When one of the operands is a String, the + operator performs string concatenation instead of arithmetic addition.
In the given expression, 5.45 is a numeric literal of type double, while "3.2" is a string literal. Therefore, Java converts the numeric value 5.45 into its string representation and joins it with "3.2".
The result of expression 5.45 + "3.2" is string "5.453.2".
| Expression | Result | Explanation |
|---|---|---|
| 5.45 + 3.2 | 8.65 | Both operands are numeric, so addition occurs. |
| 5.45 + "3.2" | "5.453.2" | One operand is a string, so concatenation occurs. |
| "5.45" + "3.2" | "5.453.2" | Both operands are strings, so concatenation occurs. |
| "5.45" + 3.2 | "5.453.2" | The numeric operand is converted to string and concatenated. |
Q: 2What is output of below Java piece of code?
public static void main(String[] args)
{
byte X=127;
X++;
X++;
System.out.print(X);
}
Option A
In Java, a byte is an 8-bit signed integer that can store values from -128 to 127. When a byte reaches its maximum value 127 and is incremented, it wraps around to the minimum value -128. This happens because Java signed integer types use 2’s complement representation.
In the given code, X is initially assigned the value 127. When X++ is executed for the first time, the value changes from 127 to -128. When X++ is executed again, the value changes from -128 to -127.
Q: 3Consider the following statements in Java:
int x=10, y=15;
x=((x>y)?(y+x):(y-x));
What will be the value of x after executing these statements?
Option D
The conditional operator (?:) is a ternary operator in Java because it works with three operands. It first evaluates a condition. If the condition is true, the expression after ? is selected; if the condition is false, the expression after : is selected.
In the given statement, the initial values are x=10 and y=15. The condition x>y is evaluated first, which means 10>15. This condition is false, so the expression after the colon : is selected, i.e., y-x.
Therefore, y-x is 15-10 is 5. This value is then assigned to x, so the final value of x becomes 5.
Q: 4Which operator in Java takes three operands?
Option B
In Java, the conditional operator (?:) is the only operator that takes three operands, so it is also called the ternary operator.
The three operands are a condition, a value to be returned when the condition is true, and a value to be returned when the condition is false.
Syntax:
condition ? expression1 : expression2
E.g.:
class Example
{
public static void main(String args[])
{
int a = 11;
int b = 22;
int result = (a>b)?a:b;
System.out.println(result);
}
}
OUTPUT
22
Here, the conditional operator checks whether a>b. Since 11>21 is false, the value after : (b) is selected. Therefore, the output is 22.
Q: 5State the output
class Example
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x=a/(b-c);
System.out.println(“x=”+x);
int y=a/(b+c);
System.out.println(“y=”+y);
}
}
Option D
In Java, the division operator (/) is used to divide one value by another. When integer values are divided, the divisor must not be zero. If an integer is divided by zero, Java generates an ArithmeticException during program execution.
Let us evaluate the statements step by step. First, the variables are initialized with the values a=10, b=5, and c=5. The program then evaluates the statement int x=a/(b-c);.
First, the expression (b-c) is evaluated. Since b is 5 and c is 5, we get 5-5 = 0. Therefore, the statement effectively becomes int x=10/0.
In Java, an integer cannot be divided by zero. When the program attempts to perform 10 / 0, Java throws an ArithmeticException: / by zero at runtime. Because the exception occurs at this statement, program execution stops immediately.
Q: 6Which operator has higher precedence?
Option B
In Java, operators have different levels of precedence, which determines the order in which they are evaluated when an expression contains multiple operators.
Among the operators given in the question, the logical AND (&&) operator has higher precedence than the logical OR (||), conditional (?:), and assignment (=) operators.
The precedence order of the given operators, from higher to lower, is && → || → ?: → =.
Note:
For example, consider the statement boolean result = true || false && false;. Since the && operator has higher precedence than the || operator, Java first evaluates false && false, which results in false. It then evaluates true || false, which results in true. Therefore, the value assigned to result is true.
Q: 7What is the output of the given java code?
byte x=64,y;
y=(byte)(x<<4);
system.out.println(y);
Option A
In Java, the byte data type occupies 8 bits and can store values from -128 to 127. In the given statement, x is declared as a byte and its value is 64.
byte x = 64, y;
y = (byte)(x << 4);
System.out.println(y);
When a byte value is used in a shift operation, Java automatically promotes it to an int. Therefore, the operation x<<4 is actually performed as an integer operation.
The value of x is 64. Shifting it four positions to the left is equivalent to multiplying it by 24. Means, 64<<4 = 64*24 = 64*16 = 1024. Thus, before the type conversion, the result is 1024.
However, the result is explicitly converted back to byte using, (byte)(1024). A byte contains only 8 bits, so only the lower 8 bits of the integer value are retained.
The binary representation of 1024 is 00000000 00000000 00000100 00000000
The lower 8 bits are, 00000000, which represents 0. Therefore, y gets the value 0, and the println statement displays 0.
Q: 8What is the output of the following code?
int a = 10;
boolean b = a++ > 10 && ++a > 11;
System.out.println(a);
Option B
In Java, the && operator uses short-circuit evaluation. This means that the second condition is evaluated only if the first condition is true. In the given code, a initially has the value 10.
First, a++>10 is evaluated. Since a++ is a post-increment operator, the current value 10 is used for the comparison, and only after that a is increased to 11. Therefore, the comparison becomes 10>10 which is false.
Because the first condition of && is false, Java does not evaluate the second condition ++a > 11. This is the short-circuit behavior of the && operator. Consequently, a remains 11.
Q: 9What will be the output of the following Java code?
public static void main(String args[])
{
byte p=32;
int i;
byte q;
i=p<<3;
q=(byte)(p<<3);
System.out.print(i+” “+q);
}
Option A
In Java, when you apply a Bitwise Left Shift (<<) to a byte, the value is first promoted to int before the shift. The result of p<<3 is computed in int arithmetic, but if you cast it back to byte, any bits outside the byte range are truncated, which can cause overflow and produce a different value.
Here, p = 32, So p << 3 means multiply by 23 = 8. So, 32*8=256. Since Java automatically promotes the result of bitwise operations to int, the value of i becomes 256.
Next, when assigning the same result to byte q using q=(byte)(p<<3), the value 256 is cast back to a byte. However, a byte can store only 8 bits, so the higher bits are truncated, leaving 0000 0000, which equals 0. Therefore, the final output printed is 256 0.
Q: 10What will be the output of the following Java program?
class Example
{
public static void main(String args[])
{
int a=50;
double b=50.1;
boolean x=(a==b);
System.out.println(x);
}
}
Option C
In Java, the == operator is used to compare two values. When an int and a double are compared using ==, Java performs numeric promotion. The int value is converted to double before the comparison.
In the given program, a has the value 50 and b has the value 50.1. In expression boolean x=(a==b);, before comparison, a is promoted from int to double. Thus, the comparison effectively becomes 50.0==50.1
Since 50.0 and 50.1 are different values, the condition is false. Therefore, x stores false, and the println() statement displays false.
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.