Skip to content
QuiznExam

Operators and Assignments in Java Quiz

Test your knowledge of operators and assignments in Java, including arithmetic, relational, logical, bitwise, assignment, unary, conditional, and shift operators, along with operator precedence and type conversions.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. Which operator is used to calculate the remainder after integer division in Java?

    • A. /
    • B. %
    • C. //
    • D. **
    Show answer

    B. %

    The modulo operator (%) returns the remainder after division. For example, 10 % 3 evaluates to 1.

  2. 2. What is the output of the following code? int x = 5; System.out.println(x++);

    • A. 5
    • B. 6
    • C. 4
    • D. Compilation error
    Show answer

    A. 5

    The postfix increment operator returns the current value before incrementing the variable. The expression prints 5, and x becomes 6 afterward.

  3. 3. Which operator checks whether two primitive numeric values are equal?

    • A. =
    • B. !=
    • C. ==
    • D. equals=
    Show answer

    C. ==

    The == operator compares primitive values for equality. For reference types, it compares object references rather than object contents.

  4. 4. What is the result of the expression true && false in Java?

    • A. true
    • B. null
    • C. Compilation error
    • D. false
    Show answer

    D. false

    The logical AND operator (&&) evaluates to true only when both operands are true.

  5. 5. Which assignment operator adds a value to a variable and assigns the result back to that variable?

    • A. ++
    • B. +=
    • C. ==
    • D. =>
    Show answer

    B. +=

    The += compound assignment operator adds the right-hand value to the variable and stores the result back in that variable.

  6. 6. What is the output of the following code? int x = 10; x += 5; System.out.println(x);

    • A. 10
    • B. 5
    • C. 15
    • D. 50
    Show answer

    C. 15

    The statement x += 5 is equivalent to x = x + 5 in this example, so x becomes 15.

  7. 7. Which operator represents logical OR with short-circuit evaluation in Java?

    • A. ||
    • B. |
    • C. &&
    • D. &
    Show answer

    A. ||

    The || operator evaluates the right operand only if the left operand is false.

  8. 8. What is the result of the expression 5 > 3 ? 10 : 20 in Java?

    • A. 3
    • B. 20
    • C. 5
    • D. 10
    Show answer

    D. 10

    The conditional operator evaluates the condition first. Since 5 > 3 is true, the expression returns 10.

  9. 9. Which operator performs a bitwise XOR operation in Java?

    • A. &
    • B. ^
    • C. |
    • D. ~
    Show answer

    B. ^

    The ^ operator performs bitwise exclusive OR on integral operands, producing 1 for each bit position where the corresponding bits differ.

  10. 10. What is the result of the expression 8 >> 1 in Java?

    • A. 16
    • B. 8
    • C. 4
    • D. 2
    Show answer

    C. 4

    The signed right-shift operator moves the bits one position to the right. For the positive integer 8, shifting right by one produces 4.

  11. 11. Which operator has higher precedence in Java?

    • A. *
    • B. +
    • C. ==
    • D. ||
    Show answer

    A. *

    Multiplication has higher precedence than addition, equality comparison, and logical OR.

  12. 12. What is the output of the following code? int x = 7; System.out.println(++x);

    • A. 7
    • B. 6
    • C. Compilation error
    • D. 8
    Show answer

    D. 8

    The prefix increment operator increments x before producing the value of the expression, so the code prints 8.

  13. 13. Which statement correctly describes the difference between = and == in Java?

    • A. Both operators perform assignment
    • B. = assigns a value, while == tests equality
    • C. = tests equality, while == assigns a value
    • D. Neither operator can be used with integers
    Show answer

    B. = assigns a value, while == tests equality

    The assignment operator (=) stores a value in a variable, whereas the equality operator (==) compares values or reference identities.

  14. 14. What is the result of the expression 6 & 3 in Java?

    • A. 7
    • B. 5
    • C. 2
    • D. 0
    Show answer

    C. 2

    In binary, 6 is 110 and 3 is 011. Their bitwise AND is 010, which equals 2.

  15. 15. What happens when the expression int x = 5 / 2; is evaluated in Java?

    • A. x becomes 2
    • B. x becomes 2.5
    • C. A compilation error occurs because division always returns a double
    • D. x becomes 3
    Show answer

    A. x becomes 2

    Both operands are integers, so Java performs integer division and discards the fractional part. The result is 2.