Javatpoint Logo
Javatpoint Logo

Operators in Java MCQ

Java is a popular programming language that is widely used for developing applications in various domains such as web development, mobile app development, and more. In Java, operators are used to perform various operations on variables and values. In this section, we will discuss frequently asked multiple-choice questions (MCQs) on operators in Java, along with their answers and explanations.

1) Which of the following is a relational operator in Java?

  1. +
  2. =
  3. ==
  4. &&

Answer: c

Explanation: The relational operator in Java is used to compare two values. The == operator is used to check if two values are equal or not.


2) Which of the following operators is used to perform addition in Java?

  1. +
  2. -
  3. *
  4. /

Answer: a

Explanation: The + operator is used to perform addition in Java.


3) Which of the following operators is used to perform division in Java?

  1. %
  2. &
  3. |
  4. /

Answer: d

Explanation: The / operator is used to perform division in Java.


4) Which of the following is a logical operator in Java?

  1. ++
  2. /
  3. &&
  4. =

Answer: c

Explanation: The logical operator in Java is used to perform logical operations such as AND, OR, and NOT. The && operator is used to perform the logical AND operation.


5) Which of the following is a unary operator in Java?

  1. +
  2. /
  3. -
  4. *

Answer: c

Explanation: The unary operator in Java is used to operate on a single operand. The - operator is used to perform negation or change the sign of a value.


6) Which of the following operators has the highest precedence in Java?

  1. +
  2. /
  3. ++
  4. &&

Answer: c

Explanation: The increment operator (++) has the highest precedence in Java.


7) Which of the following operators is used to perform modulo division in Java?

  1. /
  2. %
  3. *
  4. &

Answer: b

Explanation: The % operator is used to perform modulo division in Java.


8) Which of the following operators is used to perform bitwise AND in Java?

  1. &
  2. &&
  3. |
  4. ^

Answer: a

Explanation: The & operator is used to perform bitwise AND in Java.


9) Which of the following operators is used to perform equality comparison in Java?

  1. =
  2. ==
  3. !=
  4. >

Answer: b

Explanation: The == operator is used to perform equality comparison in Java.


10) Which of the following operators is used to perform bitwise OR in Java?

  1. |
  2. ^
  3. &
  4. ||

Answer: a

Explanation: The | operator is used to perform bitwise OR in Java.


11) What is the output of the following code snippet?

  1. 0 1 2
  2. 1 2 3
  3. 0 1 2 3
  4. Infinite loop

Answer: a

Explanation: The output of the code snippet int i = 0; while (i < 3) { System.out.print(i + " "); i++; } is 0 1 2 because the while loop will iterate three times (until i is equal to 3), and on each iteration, it will print the current value of i and increment it by 1.


12) Which of the following is not a valid primitive data type in Java?

  1. int
  2. float
  3. double
  4. string

Answer: d

Explanation: string is not a valid primitive data type in Java. The correct spelling for the string data type is String (capitalized).


13) What is the output of the following code snippet?

  1. x is less than y
  2. x is greater than or equal to y
  3. Compilation error
  4. Runtime error

Answer: a

Explanation: The output of the code snippet int x = 5; int y = 10; if (x < y) { System.out.println("x is less than y"); } else { System.out.println("x is greater than or equal to y"); } is x is less than y because x is less than y, so the if block is executed and prints the corresponding message.


14) Which of the following operators is used to perform decrement in Java?

  1. --
  2. +
  3. *
  4. &

Answer: a

Explanation: The decrement operator (--) is used to decrease the value of a variable by 1 in Java.


15) Which of the following operators is used to perform logical OR in Java?

  1. &
  2. &&
  3. |
  4. ^

Answer: c

Explanation: The | operator is used to perform logical OR in Java.


16) Whatt is the output of the following code snippet?

  1. 2
  2. 2.5
  3. 3
  4. Compilation error

Answer: a

Explanation: The output of the code snippet int a = 5; int b = 2; int c = a / b; System.out.println(c); is 2 because integer division is performed, which truncates the decimal part.


17) Which of the following operators is used to perform left shift in Java?

  1. <<
  2. >>
  3. &
  4. |

Answer: a

Explanation: The << operator is used to perform left shift in Java.


18) Which of the following is not a valid identifier in Java?

  1. $test
  2. _test
  3. 123test
  4. test123

Answer: c

Explanation: Identifiers in Java cannot start with a digit. Therefore, 123test is not a valid identifier.


19) Which of the following is a conditional operator in Java?

  1. =
  2. *
  3. +
  4. ?

Answer: d

Explanation: The conditional operator (also known as ternary operator) in Java is represented by the ? symbol. It is used to evaluate a boolean expression and return one of two values based on the result.


20) What is the output of the following code snippet?

  1. 31
  2. 32
  3. 33
  4. 34

Answer: c

Explanation: The output of the code snippet int x = 10; int y = 20; int z = x++ + ++y; System.out.println(z); is 33. The value of z is computed as 10 + 21 (x++ returns the original value of x, while ++y increments y before its value is used in the expression).


21) Which of the following is a unary logical operator in Java?

  1. !
  2. &
  3. |
  4. ^

Answer: a

Explanation: The ! operator is a unary logical operator in Java. It is used to perform the logical NOT operation.


22) What is the output of the following code snippet?

  1. 0 1 2
  2. 1 2 3
  3. 0 1 2 3
  4. Infinite loop

Answer: a

Explanation: The output of the code snippet int i = 0; do { System.out.print(i + " "); i++; } while (i < 3); is 0 1 2 because the do-while loop will iterate three times (until i is equal to 3), and on each iteration, it will print the current value of i and increment it by 1.


23) Which of the following operators is used to perform bitwise XOR in Java?

  1. ^
  2. |
  3. &
  4. ~

Answer: a

Explanation: The ^ operator is used to perform bitwise XOR (exclusive OR) in Java. It returns a 1 in each bit position where the corresponding bits of either but not both operands are 1.


24) What is the output of the following code snippet?

  1. a is greater than b
  2. a is less than or equal to b
  3. Compilation error
  4. Runtime error

Answer: b

Explanation: The output of the code snippet int a = 5; int b = 7; System.out.println((a > b) ? "a is greater than b" : "a is less than or equal to b"); is a is less than or equal to b because the expression (a > b) is false, so the second option in the ternary operator is executed.


Next TopicSeparators In Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA