Java Operators MCQ

Java, as an object-oriented programming language, is rich in its use of operators. Operators allow us to perform various operations on variables and values. If you're new to Java, understanding these operators is vital for further coding adventures. Dive into this Java Operators Quiz to test your foundational knowledge!

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. Which operator is used for concatenating two strings in Java?

a) +
b) &
c) *
d) ^

Answer:

a) +

Explanation:

In Java, the + operator is overloaded for string concatenation.

2. What will be the result of the expression 3 + 6 * 2?

a) 18
b) 15
c) 12
d) 21

Answer:

b) 15

Explanation:

According to operator precedence, multiplication is evaluated before addition. Thus, 6 * 2 equals 12, and then 3 is added, resulting in 15.

3. Which operator is used in Java for bitwise AND operation?

a) &&
b) &
c) and
d) AND

Answer:

b) &

Explanation:

The & operator is used for bitwise AND operations, while && is used for logical AND operations.

4. Which operator checks if two references point to the same object in memory?

a) =
b) ==
c) equals
d) ===

Answer:

b) ==

Explanation:

In Java, == checks if two references point to the same memory location. The equals method is used to check for value equality, and there is no === operator in Java.

5. Which of the following is a postfix increment operator?

a) ++x
b) x++
c) x+
d) +x

Answer:

b) x++

Explanation:

x++ is the postfix increment operator. It increments the value of x after its current value is used.

6. Which operator can be used to shift bits to the left?

a) <<
b) >>
c) <<<
d) >>>

Answer:

a) <<

Explanation:

The << operator is used to left shift the bits of a number.

7. Which of the following operators is ternary?

a) +
b) :
c) ? :
d) /

Answer:

c) ? :

Explanation:

The ternary operator is represented by ? : and is the only operator in Java that takes three operands.

8. How can you check if a number x is even using operators?

a) x % 2 = 0
b) x / 2 = 0
c) x - 2 = 0
d) x * 2 = 0

Answer:

a) x % 2 = 0

Explanation:

If a number is divisible by 2 with no remainder (x % 2), then it's an even number.

9. Which of the following is a modulus operator in Java?

a) %
b) /
c) #
d) @

Answer:

a) %

Explanation:

The % operator returns the remainder of a division operation.

10. What is the output of the following program?


public class Main {
    public static void main(String[] args) {
        int x = 5;
        int y = 2;
        int result = x % y;
        System.out.println(result);
    }
}
a) 2
b) 2.5
c) 1
d) 0

Answer:

c) 1

Explanation:

The % operator is used for finding the remainder of the division operation. In this case, 5 divided by 2 leaves a remainder of 1.

I hope this quiz was both challenging and enlightening. Remember, mastering operators is a fundamental step in mastering Java. Keep practicing and continue your learning journey!



Comments