Question : You have been given below code, please select the correct option which applies to it.
package com.hadoopexam;
import java.util.*;
class Welcome { public static void main(String []args) { List hList = new ArrayList<>(); hList.add(1); hList.add(2); System.out.println("Contents in list are : " + hList); } } 1. Code will compile and run perfectly and print to "Contents in list are : [1,2]"
2. Code will compile and run perfectly and print to "Contents in list are : [2,1]"
3. Code will compile successfully but will produce run time error IllegalStateException
4. Code will not compile
Correct Answer : 4 Explanation: Yes code, will not compile. Because you cannot use Int primitive in as generic. You have to change primitive int to Integer class.
Question : You have been given below code, what would be printed once you run the code.
package com.hadoopexam;
import java.util.*;
class Welcome { public static void main(String[] args) { List integerList = new LinkedList<>(); List doublelList = new LinkedList<>(); System.out.println("Type of integerList = " + integerList.getClass()); System.out.println("Type of doublelList = " + doublelList.getClass()); } } 1. It will print following statements Type of integerList = class java.util.LinkedList Type of doublelList = class java.util.LinkedList
2. It will print following statements Type of integerList = class java.util.LinkedList<> Type of doublelList = class java.util.LinkedList<>
3. It will print following statements Type of integerList = class java.util.LinkedList Type of doublelList = class java.util.LinkedList
4. It will print following statements Type of integerList = class java.util.List Type of doublelList = class java.util.List
Correct Answer : 1 Explanation: As you know, Generics are compile time only. Hence, all the Generic information can be removed once the code is compiled and byte code is generated. As well as there is a dynamic binding so at run time actual class will be used when you refer an object. Here LinkedList is the actual data type for both list.
Question : You have been given following code, please select the correct statement for it.
package com.hadoopexam;
import java.util.Arrays;
class Welcome { public static void main(String[] args) { String[] names = { "Bala", "Reena", "Iva", "Cate" }; Arrays.sort(names, null); //n1 for (String name : names) { System.out.print(name + " "); } } }
1. Code will not compile.
2. Code will compile but produce runtime error. As no InvalidComparatorException is defined.
3. There will be no compile time error. Code will be run successfully and print Bala Cate Iva Reena
4. There will be no compile time error. Code will be run successfully and print "Bala", "Reena", "Iva", "Cate"
Correct Answer : 3 Explanation: Code will compile and run successfully and print Bala Cate Iva Reena. When null is passed as a second argument to the Arrays.sort() method, it means that the default Comparable (i.e., laxographical ordering for the elements) should be used. The default Comparator results in sorting the elements in ascending order.