Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which line rests on a mistaken assumption?

public class Sample85
{
    public static void main(String[] args)
    {
        assert -7 % 3 == 2 : -7 % 3;
        System.out.println(-7 % 3);
    }
}

  • A.assert -7 % 3 == 2 : -7 % 3;
  • B.System.out.println(-7 % 3);
  • C.public static void main(String[] args)
  • D.Nothing here is wrong.

Answer: A

In Java the remainder takes the sign of the dividend, so -7 % 3 is -1, not 2. The assertion assumes the mathematical modulus, which is always non-negative. Getting a non-negative result requires adding the divisor back: ((-7 % 3) + 3) % 3.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.