SCJP 1.6版考題 189

出自 陳富國維基館
前往: 導覽搜尋
23. Object[] myObjects = {
24. new Integer(12),
25. new String("foo"),
26. new Integer(5),
27. new Boolean(true)
28. };
29. Arrays.sort(myObjects);
30. for(int i=0; i<myObjects.length; i++){
31. System.out.print(myObjects[i].toString());
32. System.out.print(" ");
33. }

What is the result?
 A. Compilation fails due to an error in line 23. 
 B. Compilation fails due to an error in line 29.
 C. A ClassCastException occurs in line 29.
 D. A ClassCastException occurs in line 31.
 E. The value of all four objects prints in natural order.

解答


Ans: C

解說: myObjects這個集合物件裏不同型態的物件,不同型態的物件排序時(需進行比較)會產生例外。(第29行)