Oracle
1Z0-851 · Question #102
Given: 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++)…
The correct answer is C. A ClassCastException occurs in line 29. Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at java.lang.Integer.compareTo(Unknown Source) at java.util.Arrays.mergeSort(Unknown Source) at java.util.Arrays.sort(Unknown Source) at Barn.main(Barn.java:29)
Analyzing and Troubleshooting System Problems
Question
Given: 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?
Options
- ACompilation fails due to an error in line 23.
- BCompilation fails due to an error in line 29.
- CA ClassCastException occurs in line 29.
- DA ClassCastException occurs in line 31.
- EThe value of all four objects prints in natural order.
How the community answered
(26 responses)- A4% (1)
- B15% (4)
- C73% (19)
- D8% (2)
Explanation
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at java.lang.Integer.compareTo(Unknown Source) at java.util.Arrays.mergeSort(Unknown Source) at java.util.Arrays.sort(Unknown Source) at Barn.main(Barn.java:29)
Topics
#Java Arrays.sort#ClassCastException#Comparable interface#mixed-type array
Community Discussion
No community discussion yet for this question.