Expressions and Assignment Statements

AP Computer Science A· difficulty 3/5

int x = 1 + 2 * 3;
System.out.println(x);

What does this print, and is the code correct?

  • A

    Prints 6; addition is performed first

  • B

    Compile error

  • C

    Prints 7; multiplication has higher precedence than addition

    check_circle
  • D

    Prints 9; left-to-right evaluation

Explanation

Java follows standard math precedence: 2 * 3 = 6, then 1 + 6 = 7.

Want 10 more like this — adaptive to your weak spots?

Related questions