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

Discussion

Which line will cause a compilation error?

public class Sample88
{
    private final java.util.Map<String, Integer> tally =
        new java.util.HashMap<String, Integer>();

    void record(String key)
    {
        Assert key != null;
        tally.put(key, tally.size() + 1);
    }

    public static void main(String[] args)
    {
        Sample88 s = new Sample88();
        s.record("first");
        System.out.println(s.tally);
    }
}

  • A.tally.put(key, tally.size() + 1);
  • B.s.record("first");
  • C.The code is correct as it stands.
  • D.Assert key != null;

Answer: D

Java keywords are lowercase, so Assert with a capital A is read as a type name and the line does not compile. There is no such class in scope. The keyword is simply assert, and no import can make the capitalised form work.

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

Post your comment

Please log in or register to post a comment.