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

Discussion

Which line rests on a mistaken assumption?

public class Sample36
{
    static double ratio(int num, int den)
    {
        return (double) num / den;
    }

    public static void main(String[] args)
    {
        double r = ratio(0, 0);
        assert r == r : "not itself";
        System.out.printf("%s%n", r);
    }
}

  • A.double r = ratio(0, 0);
  • B.return (double) num / den;
  • C.The code is correct as it stands.
  • D.assert r == r : "not itself";

Answer: D

Dividing zero by zero in floating point gives NaN, and NaN compares unequal to everything including itself, so this assertion fails on an expression that looks tautological. Testing for NaN needs Double.isNaN(d). Note the division does not throw, unlike integer division by zero.

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

Post your comment

Please log in or register to post a comment.