Java OCA OCP Practice Question 2455

Question

Given:

2. import java.util.*;  
3. public class Main {  
4.   public static void main(String[] args) {  
5.     Map<Employee, String> hm = new HashMap<Employee, String>();  
6.     hm.put(new Employee("Charis"), "Summer 2009");  
7.     hm.put(new Employee("Jack"), "Spring 2002");  
8.     Employee f = new Employee(args[0]);  
9.     System.out.println(hm.get(f));  
10.   }  /*from  ww w . ja  va  2 s. c om*/
11. }  
12. class Employee {  
13.   String name;  
14.   Employee(String n) { name = n; }  
15. } 

And the command line invocation:

java Main Jack 

What is the result?

  • A. null
  • B. Jack
  • C. Spring 2002
  • D. Compilation fails.
  • E. The output is unpredictable.
  • F. An exception is thrown at runtime.
  • G. Employee@XXXX (where XXXX is a representation of a hash code)


A is correct.

Note

The Employee class doesn't override equals() and hashCode(), so the key to the HashMap is a specific instance of Employee, not the value of a given Employee instance's name.




PreviousNext

Related