|
|
|
|
350 Mock Questions on SCJP 1.5 - JUST Rs.250 or 7 USD
Send us mail to sales@javabeat.net
more details
|
|
SCJP 5.0 - Generics Mock Exam - 3
1)what is the result compiling and running the following piece of code?
import java.util.*;
class Test {
public static void main(String [] args) {
Set vals = new TreeSet<String>();
vals.add("one");
vals.add(1);
vals.add("two");
System.out.println(vals);
}
}
a)Does not Compile
b)Compiles with warning and prints output [one, 1, two]
c)Compiles without warning and prints output [one, 1, two]
d)Compiles with warning and throws exception at runtime
e)Compiles without warning and throws exception at runtime
Answer:
1) D
Compiles with warning due to un-safe assignment List vals = new ArrayList<String>();
Since TreeSet<String> is used, it will try to sort by natural order. Due to the presence of Integer (vals.add(1);) in the collection, it will throw ClassCastException at runtime(While try to cast Integer in to String).
|
|
350 Mock Questions on SCJP 1.5 - JUST Rs.250 or 7 USD
Send us mail to sales@javabeat.net
more details
|
|
|