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

Discussion

Which line will cause a compilation error?

public class Sample07
{
    static int sqrtFloor(int v)
    {
        assert v > 0 : "v was " : v;
        return (int) Math.sqrt(v);
    }

    public static void main(String[] args)
    {
        System.out.println(sqrtFloor(16));
    }
}

  • A.System.out.println(sqrtFloor(16));
  • B.Every line above compiles as written.
  • C.assert v > 0 : "v was " : v;
  • D.return (int) Math.sqrt(v);

Answer: C

The assert statement takes at most two expressions, separated by a single colon. A second colon is a syntax error. To include the value in the message, build one expression instead: assert n > 0 : "n was " + n;.

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

Post your comment

Please log in or register to post a comment.