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

Discussion

Which line rests on a mistaken assumption?

public class Sample43
{
    public static void main(String[] args)
    {
        assert Math.round(-2.5) == -3 : Math.round(-2.5);
        System.out.println(Math.round(-2.5));
    }
}

  • A.public static void main(String[] args)
  • B.Every line above is correct as written.
  • C.assert Math.round(-2.5) == -3 : Math.round(-2.5);
  • D.System.out.println(Math.round(-2.5));

Answer: C

Math.round adds one half and takes the floor, so a value exactly halfway rounds toward positive infinity: -2.5 becomes -2, not -3. The assertion assumes rounding moves away from zero on both sides, which holds for positive halves only.

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

Post your comment

Please log in or register to post a comment.