5. TreeMap is always sorted by Keys. Map can not have duplicate keys, but it can have duplicate values.
java.util.TreeMap.TreeMap()
Constructs a new, empty tree map, using the natural ordering of its keys. All keys inserted into the map must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any keys k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint (for example, the user attempts to put a string key into a map whose keys are integers), the put(Object key, Object value) call will throw a ClassCastException.
Correct Answer : 1 Explanation:
Question : You have been given below code, what behavior are you expecting?
package com.hadoopexam;
interface ParentInterface { default void call() { System.out.println("Why are you not working?"); } }
class Welcome { public static void main(String[] args) { ChildInterface cInterface = () -> System.out.println("Welcome to HadoopExam Learning Resources"); cInterface.call(); } } 1. There will be a compile time error, as child interface can not override parent interface abstract method.
2. It will given compile time error, because ChildInterface is not a correctr functional interface.
3. This program compile and run perfectly and prints Why are you not working?.
4. This program compile and run perfectly and prints Welcome to HadoopExam Learning Resources.
Correct Answer : 4 Explanation: A default method of a parent interface can be overridden by a child interface and child interface can mark is abstract. And given method call to call() method will call lambda expression. Hence, it will print respective output.
Question : You have been given below code, what is the behavior you are expecting?
public boolean equals(Object obj) { // line n1 boolean output = false; Welcome b = (Welcome) obj; if (this.name.equals(b.name)) output = true; return output; }
public static void main(String[] args) { Welcome wel1 = new Welcome(9199, "Hadoop User"); Welcome wel2 = new Welcome(2099, "Hadoop User"); System.out.println(wel1.equals(wel2)); // line n2 }
}
1. It will compile time error, as you have not overridden the hashCode() method.
2. It will give compile time error, you have to replace System.out.println(wel1.equals((Object)wel2));
3. It will compile and run, and print true
4. It will compile and run and print false.
Correct Answer : 3 Explanation: Given code is perfectly fine. To compare two objects you need to override equals() method. hashCode() method is required only when you want to store these objects in HashMap, HashSet which uses hash values to find the correct address of the stored objects in collection. Here, we are using course name to check whether two objects are same or not. As we can see both are using same name hence, they are equal and it will print true.