Java Operators Coding Questions and Answers

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);
a) 0
b) 1
c) 2
d) 3

Answer:

a) 0

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);
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

b) false

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);
a) 0
b) 10
c) 20
d) 30

Answer:

b) 10

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);
a) 1
b) 1.66
c) 2
d) 2.5

Answer:

a) 1

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);
a) 4 4
b) 5 4
c) 4 5
d) 5 5

Answer:

b) 5 4

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);
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

a) true

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);
a) true
b) false
c) Compilation error
d) Runtime error

Answer:

a) true

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);
a) 5
b) 10
c) 15
d) 25

Answer:

d) 25

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);
a) 12
b) 5
c) 13
d) 8

Answer:

c) 13

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);
a) 11
b) 24
c) 64
d) 1024

Answer:

c) 64

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