OOPS.................

Thursday, January 1, 2009

What will happen when you attempt to compile and run the following code?

import java.util.*;
public class VecOrder{
public static void main(String argv[]){
new VecOrder();
}
VecOrder(){
Vector child = new Vector();
child.add("harry");
child.add("molly");
child.add("steven");
child.add("helen");
Collections.sort(child);
for(String element : child){
System.out.print(element);
}
}
}


Answers:...........
1 :Compilation error, problem with creation of instance of Vector class
2 :Compilation but runtime error, the Collections.sort method can only sort objects of type List
3:Compilation error, the for loop structure needs to cast each element of the Vector array to a String
4:Compilation and output of harryhelenmollysteven

No comments: