public class Welcome { public static void main(String[] args) { Map map = new HashMap<>(); map.put(1, "a"); map.put(5, "b"); map.put(30, "c"); map.put(70, "d"); map.put(15, "e"); Map treeMap = new TreeMap( new Comparator() { @Override public int compare(Integer o1, Integer o2) { return o2.compareTo(o1); } }); treeMap.putAll(map); for (Map.Entry entry : treeMap.entrySet()) { System.out.print(entry.getValue() + " "); } } } 1. It will give compile time error
2. It will compile nut produce , runtime error
3. It will compile and run perfectly and produce a,b,c,d,e
4. It will compile and run perfectly and produce e,d,c,b,a
5. It will compile and run perfectly and produce d c e b a
Correct Answer : 5 Explanation: We have first stored all the key values in map like (1,a)(5,b), (30,c) ,(70,d), (15,e). We are using TreeMap, which help us sort the map values based on keys and not on values. However, we have overridden the TreeMap compare() method, which sort the values in reverse order. Hence, keys would be sorted like (70,30,15,5,1) . So when we iterate on map it will generate values as d c e b a
Question : What is the behavior of the following code? package com.hadoopexam;
class Parent { } class Child1 extends Parent { } class Child2 extends Parent { } class GrandChild extends Child1, Child2 { }
1. It will compile and run perfectly.
2. It will compile perfectly but give NullPointerException when run
3. It will not compile correctly.
4. It will not compile and there will be an exception e.g. ClassNotFoundException.
Correct Answer : 3 Explanation: As we know, java does not support multiple inheritance. Hence, code will not compile, because you are trying to extends two classes in GrandChild class.
Question : Which two reasons should you use interfaces instead of abstract classes? A. You expect that classes that implement your interfaces have many common methods or fields, or require access modifiers other than public. B. You expect that unrelated classes would implement your interfaces. C. You want to share code among several closely related classes. D. You want to declare non-static on non-final fields. E. You want to take advantage of multiple inheritance of type
2. It will also run without any error and print "In Company"
3. It will also run without any error and print "In Welcome"
4. It will not compile as it will give error at line n1.
5. If you want to create an instance of an inner class. Then you also have to create instance of an outer class. As below Welcome. Company comp = new Welcome().new Company();