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

Discussion

Which line will cause a compilation error?

public class Sample89
{
    private final java.util.List<String> queue =
        new java.util.LinkedList<String>();

    void drain()
    {
        while (!queue.isEmpty())
        {
            assert;
            queue.remove(0);
        }
    }

    public static void main(String[] args)
    {
        new Sample89().drain();
    }
}

  • A.assert;
  • B.queue.remove(0);
  • C.while (!queue.isEmpty())
  • D.Nothing here is wrong.

Answer: A

Correct answer: assert;

The assert statement requires a boolean expression - either assert expr; or assert expr : detail; - so a bare assert; is a syntax error. The rest of the method is sound: queue.isEmpty() and queue.remove(0) are both valid on a List, and removing index 0 repeatedly is a legitimate way to drain one.

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

Post your comment

Please log in or register to post a comment.