Java MCQs – Operators and Expressions

11.) What is an expression in Java?

A) A combination of variables, literals, operators, and method calls that produces a value.
B) An access modifier that controls the visibility of a class.
C) A block of code used to define a method’s implementation.
D) A reserved keyword used to declare a variable.

Answer: Option A

Explanation: An expression in Java is a combination of variables, literals, operators, and method calls that results in a single value. It can represent a mathematical operation, a comparison, or any computation that produces an outcome.

12.) What is the purpose of parentheses in an expression?

A) To specify the order of evaluation.
B) To declare variables.
C) To indicate a comment in the code.
D) To control the access modifiers of methods.

Answer: Option A

Explanation: Parentheses in an expression are used to specify the order of evaluation. Operations within parentheses are evaluated before operations outside them, overriding the normal operator precedence.

13.) Which of the following is NOT a valid Java expression?

A) 2 + 0
B) “Hello, ” + “world!”
C) if (x > 0) { x = x + 1; }
D) x = 10;

Answer: Option C

Explanation: The expression in option c is a statement rather than an expression. Expressions produce values, but statements are used to perform actions or control the flow of a program. The correct expression would be just “x > 0” without the “if” statement.

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

A) <<
B) >>
C) ++
D) —

Answer: Option A

Explanation: In Java, the “<<” operator is used to perform the left shift.

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

A) !
B) ^
C) &
D) |

Answer: Option A

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

Leave a Reply

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