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

Discussion

What will be the output of the program?

import java.util.*;

public class Case32
{
    public static void main(String[] args)
    {
        Map<String, String> m = new Hashtable<String, String>();
        m.put("k", null);
        System.out.println(m.size());
    }
}

  • A.The program prints 0
  • B.Compilation fails.
  • C.The program prints 1
  • D.A NullPointerException is thrown.

Answer: D

Hashtable is the legacy synchronised map and permits neither a null key nor a null value, throwing NullPointerException on either. HashMap allows one null key and any number of null values, which is the distinction being drawn.

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

Post your comment

Please log in or register to post a comment.