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

Discussion

Which line will cause a compilation error?

public class Sample77
{
    private int low, mid, high;

    void sort3(int x, int y, int z)
    {
        low = Math.min(x, Math.min(y, z));
        high = Math.max(x, Math.max(y, z));
        mid = x + y + z - low - high;
        assert low < mid < high;
    }

    public static void main(String[] args)
    {
        new Sample77().sort3(9, 2, 5);
    }
}

  • A.assert low < mid < high;
  • B.mid = x + y + z - low - high;
  • C.low = Math.min(x, Math.min(y, z));
  • D.Nothing here is wrong.

Answer: A

Java has no chained comparison. a < b produces a boolean, and comparing a boolean with the int c is a type error, so the line does not compile. The intended test is written with an explicit conjunction: assert a < b && b < c;.

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

Post your comment

Please log in or register to post a comment.