Java Operators MCQ

1. What is the primary use of operators in Java?

a) To define variables
b) To create methods
c) To perform operations on variables and values
d) To declare classes

Answer:

c) To perform operations on variables and values

Explanation:

Operators in Java are used to perform various operations on variables and values, such as arithmetic, assignment, comparison, and logical operations​“【oaicite:12】“​.

2. Which operator is used in Java for addition?

a) *
b) +
c) /
d) %

Answer:

b) +

Explanation:

The '+' operator is used for addition in Java, allowing the addition of two values, a variable and a value, or two variables​“【oaicite:11】“​.

3. What does the '++' operator do in Java?

a) Divides a value by two
b) Subtracts one from a value
c) Multiplies a value by two
d) Increments a value by one

Answer:

d) Increments a value by one

Explanation:

The '++' operator is an arithmetic operator in Java that increments the value of a variable by one​“【oaicite:10】“​.

4. What is the function of the assignment operator '=' in Java?

a) Compares two values
b) Assigns a value to a variable
c) Adds two values
d) Subtracts one value from another

Answer:

b) Assigns a value to a variable

Explanation:

The assignment operator '=' is used to assign a value to a variable in Java​“【oaicite:9】“​.

5. What does the '!=' operator do in Java?

a) Assigns a value
b) Checks for equality
c) Checks for inequality
d) Divides two values

Answer:

c) Checks for inequality

Explanation:

The '!=' operator is a comparison operator that checks if two values are not equal​“【oaicite:8】“​.

6. What is the result of using the '&&' logical operator in Java?

a) Returns true if both statements are true
b) Returns true if either statement is true
c) Concatenates two strings
d) Compares two values for equality

Answer:

a) Returns true if both statements are true

Explanation:

The '&&' operator is a logical AND operator, returning true if both the operands are true​“【oaicite:7】“​.

7. Which operator returns the remainder of a division in Java?

a) /
b) %
c) *
d) +

Answer:

b) %

Explanation:

The '%' operator, known as the modulus operator, is used in Java to return the remainder of a division​“【oaicite:6】“​.

8. How does the '+=' assignment operator function in Java?

a) Divides and assigns the result
b) Multiplies and assigns the result
c) Adds to the variable and assigns the result
d) Subtracts from the variable and assigns the result

Answer:

c) Adds to the variable and assigns the result

Explanation:

The '+=' operator adds a value to a variable and then assigns the result to that variable​“【oaicite:5】“​.

9. What is the purpose of the '<' operator in Java?

a) To check if a value is less than another
b) To assign a smaller value
c) To subtract one value from another
d) To decrement a value

Answer:

a) To check if a value is less than another

Explanation:

The '<' operator is a comparison operator used to check if a value is less than another value&#8203;“【oaicite:4】“&#8203;.

10. Which operator is used to reverse the result of a boolean expression in Java?

a) &&
b) ||
c) !
d) ==

Answer:

c) !

Explanation:

The '!' operator is a logical NOT operator, used to reverse the result of a boolean expression&#8203;“【oaicite:3】“&#8203;.

11. What is the function of the '>>=' operator in Java?

a) Left shift and assign
b) Right shift and assign
c) Bitwise OR and assign
d) Bitwise AND and assign

Answer:

b) Right shift and assign

Explanation:

The '>>=' operator is an assignment operator that performs right bitwise shift on the variable and then assigns the result&#8203;“【oaicite:2】“&#8203;.

12. What does the '==' operator check in Java?

a) If two values are not equal
b) If two values are equal
c) The sum of two values
d) The difference between two values

Answer:

b) If two values are equal

Explanation:

The '==' operator is a comparison operator used to check if two values are equal&#8203;“【oaicite:1】“&#8203;.

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

a) +
c) *
d) ^

Answer:

a) +

Explanation:

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

14. 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.

15. 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.

16. 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.

17. 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.

18. 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.

19. 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.

20. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top