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

Discussion

The program is run with java Snippet28 - what will be the output?

import java.util.*;

public class Snippet28
{
    public static void main(String[] args)
    {
        Map<String, Integer> ages = new HashMap<String, Integer>();
        ages.put("amy", 30);
        Integer gone = ages.remove("amy");
        assert gone != null;
        System.out.println(ages.isEmpty());
    }
}

  • A.An AssertionError is thrown.
  • B.Compilation fails.
  • C.The program prints false
  • D.The program prints true

Answer: D

This is the corrected form of the previous mistake. The removal happens as an ordinary statement, so it runs whether or not assertions are enabled, and only the check of its returned value sits inside the assert. The map ends up empty either way, so the printed result no longer depends on the -ea flag.

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

Post your comment

Please log in or register to post a comment.