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

Discussion

What will be the output of the program?

public class Example19
{
    void run()
    {
        double value = -1.5;
        System.out.println(Math.floor(value) + " " + Math.ceil(value));
    }

    public static void main(String[] args)
    {
        new Example19().run();
    }
}

  • A.The program prints -1.0 -1.0
  • B.The program prints -1.0 -2.0
  • C.The program prints -2.0 -1.0
  • D.The program prints -2.0 -2.0

Answer: C

floor moves toward negative infinity and ceil toward positive infinity, so -1.5 becomes -2.0 and -1.0 respectively. Both return a double, which is why the output carries the decimal point. Thinking of them as round-down and round-up in magnitude reverses the answer for negatives.

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

Post your comment

Please log in or register to post a comment.