Question : What would be the output when you run below code ?
package com.hadoopexam;
class Welcome { public void print(Integer i) { System.out.println("Integer value"); }
public void print(int i) { System.out.println("primitive int value "); }
public void print(long i) { System.out.println("primitive long value"); }
public static void main(String args[]) { Welcome wel = new Welcome(); wel.print(10); } } 1. It will print "Integer value".
2. It will print "primitive int value "
3. It will print "primitive long value"
4. It will give Runtime Error. Method Ambiguity found.
Correct Answer : 3 Explanation: The code will match with int literal value and not with the Integer Class or long primitives.
Question : You have been given below code, what is the expected behavior when executed with -ea option
package com.hadoopexam;
public class Welcome { public static void main(String[] args) { int val1 = 10; int val2 = -1; assert (val2 >= 1) : "Invalid value have been provided"; int result = val1 / val2; System.out.println(result); } }
1. It will run and produce output as -10
2. It will run and produce output as 0
3. It will run and produce output as -1
4. It will run and throw AssertionError: Invalid value have been provided
Correct Answer : 4 Explanation: As we know Assertion assumption is correct, then nothing will happen. But if assertion assumption is not correct it will throw assertion error.
Question : You have been given below two class in separate files. What is the correct replacement for the XXXXX?
package com.hadoopexam;
public class Welcome { protected void print() { System.out.println("Parent Class"); } }
package com.hadoopexam;
public class Company extends Welcome { XXXXX void display() { System.out.println("Child Class"); } }
1. You can use only abstract,
2. You can use public or protected
3. You can only use public
4. You can only use protected.
Correct Answer : 2 Explanation: : You can use either public or protected. As access modifier you cannot restrict. You can have only more wider or same access modifiers in child class. If method is overridden.
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.