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: 1Which 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: 2What 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: 3What 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: 4What 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: 5What is the output?
System.out.println(10 + 20 + "Banjara" + 10 + 20);
Option C
In Java, the + operator is used for both arithmetic addition and string concatenation. The operation is evaluated from left to right. As long as both operands are numeric, Java performs addition. Once a String is encountered, the + operator changes to string concatenation, and the remaining values are converted to strings.
In the given expression, System.out.println(10 + 20 + "Banjara" + 10 + 20); First, 10+20 is performed because both operands are integers, which is 10 + 20 = 30.
Next, 30 + "Banjara" involves a string, so concatenation takes place, 30 + "Banjara" = "30Banjara". After that, the remaining 10 and 20 are also concatenated as strings, "30Banjara" + 10 = "30Banjara10" and "30Banjara10" + 20 = "30Banjara1020"
Therefore, the final output is 30Banjara1020.
Note:
The position of the string is crucial. If the expression were System.out.println("Banjara" + 10 + 20); then the result would be Banjara1020 because once "Banjara" is encountered, all subsequent + operations perform string concatenation.
Q: 6What is the output of the following code?
public class Demo {
public static void main(String[] args) {
int x = 5;
System.out.println(++x*2);
}
}
Option A
In this code, the variable x is initially assigned the value 5. The expression ++x * 2 uses the pre-increment operator, which means x is increased before it is used in the calculation. So, ++x changes the value of x from 5 to 6, and then the multiplication 6 * 2 is performed. Therefore, the output printed by System.out.println(++x * 2); is 12.
Q: 7What is the output of the below given code:
int a = 5;
System.out.println(a++ + ++a);
Option C
In Java, the post-increment (a++) operator first uses the current value of the variable and then increases it by 1. On the other hand, the pre-increment (++a) operator first increases the value by 1 and then uses the updated value. Java evaluates the operands of the + operator from left to right.
Initially, a=5. In the expression, a++ + ++a, the first part, a++, uses the current value 5 and then increments a to 6. Next, ++a first increments a from 6 to 7 and then uses the value 7. Therefore, the expression becomes 5+7 = 12.
Q: 8What will be the output of the code after the given code executes?
int a = 5;
int b = a++;
System.out.println(“a = ”+a+”b = ”+b);
Option A
In this code, the variable a is first assigned the value 5. When the statement int b = a++; executes, the operator used is the post-increment operator (a++).
In post-increment, the current value of the variable is used first, and then the increment happens afterward. This means that the value of a is first assigned to b, and only after this assignment does a increase by 1. As a result, after the statement executes, a becomes 6 while b remains 5.
You have reached the end of this topic. Continue learning with the next topic below.
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.