Expressions and Assignment Statements

AP Computer Science A· difficulty 4/5

int a = -17;
int b = 5;
System.out.println(a % b);

What is printed?

  • A

    -2

    check_circle
  • B

    3

  • C

    2

  • D

    -3

Explanation

In Java, the result of % takes the sign of the dividend; -17 % 5 = -2 because -17 = 5 * (-3) + (-2).

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

Related questions