Welcome to the Java Operators Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge of Java Operators. Each question has a correct and brief explanation.
1. What is the output of the following Java code snippet?
int a = 6;
int b = 3;
int result = a % b;
System.out.println(result);
Answer:
Explanation:
The % operator returns the remainder of the division. Since 6 is divisible by 3 without any remainder, the result is 0.
2. What does this Java code snippet output?
int x = 5;
int y = 10;
System.out.println(x > y);
Answer:
Explanation:
The > operator checks if the left-hand operand is greater than the right-hand operand. Since 5 is not greater than 10, the result is false.
3. Identify the output of the following code:
int a = 10;
int b = 20;
System.out.println(a & b);
Answer:
Explanation:
The & operator performs a bitwise AND operation. The binary representation of 10 is 1010, and 20 is 10100. The result of 1010 & 10100 is 1010, which is 10.
4. What will be printed by this Java code?
int a = 5;
int b = 3;
int result = a / b;
System.out.println(result);
Answer:
Explanation:
In integer division, the fractional part is truncated. 5 / 3 results in 1 with the fractional part .66 being discarded.
5. What does this code snippet output?
int x = 4;
int y = x++;
System.out.println(x);
System.out.println(y);
Answer:
Explanation:
The x++ is a post-increment operation. It returns the original value of x (4) to y, and then increments x to 5.
6. What is the result of executing this code?
boolean a = true;
boolean b = false;
System.out.println(a || b);
Answer:
Explanation:
The logical OR operator || returns true if either of the operands is true. Since a is true, the overall expression evaluates to true.
7. What will the following Java code snippet output?
int a = 1;
int b = 2;
int c = 3;
System.out.println(a < b && b < c);
Answer:
Explanation:
Both conditions a < b and b < c are true, so the logical AND && operator returns true.
8. What does the following code snippet print?
int a = 15;
int b = 10;
System.out.println(a | b);
Answer:
Explanation:
The | operator performs a bitwise OR operation. The binary of 15 is 1111, and 10 is 1010. The result of 1111 | 1010 is 1111, which is 25.
9. Determine the output of this Java code:
int a = 9;
int b = 4;
System.out.println(a ^ b);
Answer:
Explanation:
The ^ operator performs a bitwise XOR operation. The binary of 9 is 1001, and 4 is 0100. The result of 1001 ^ 0100 is 1101, which is 13.
10. What is the result of the following code snippet?
int a = 8;
int b = 3;
System.out.println(a << b);
Answer:
Explanation:
The << operator is a left-shift bitwise operator. Shifting 8 (which is 1000 in binary) left by 3 places results in 1000000, which is 64.
Comments
Post a Comment
Leave Comment