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

Discussion

Which line will cause a compilation error?

public class Sample06
{
    private int limit;
    private boolean capped;

    void configure()
    {
        assert capped = true;
        assert limit = 10;
    }

    public static void main(String[] args)
    {
        new Sample06().configure();
    }
}

  • A.Compilation succeeds.
  • B.assert limit = 10;
  • C.assert capped = true;
  • D.private boolean capped;

Answer: B

An assignment is an expression whose value is the value assigned, so this one has type int and fails the boolean requirement. The same shape with a boolean variable, assert flag = true;, does compile, which is what makes this worth knowing: the single = is not itself the error.

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

Post your comment

Please log in or register to post a comment.