Oracle
1Z0-851 · Question #1
Given a pre-generics implementation of a method: 11. public static int sum(List list) { 12. int sum = 0; 13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) { 14. int i =…
The correct answer is A. Remove line 14. C. Replace line 13 with "for (int i : intList) {". F. Replace the method declaration with "sum(List<Integer> intList)". See the full explanation below for the reasoning.
Question
Given a pre-generics implementation of a method: 11. public static int sum(List list) { 12. int sum = 0; 13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) { 14. int i = ((Integer)iter.next()).intValue(); 15. sum += i; 16. } 17. return sum; 18. } What three changes allow the class to be used with generics and avoid an unchecked warning? (Choose three.)
Options
- ARemove line 14.
- BReplace line 14 with "int i = iter.next();".
- CReplace line 13 with "for (int i : intList) {".
- DReplace line 13 with "for (Iterator iter : intList) {".
- EReplace the method declaration with "sum(List<int> intList)".
- FReplace the method declaration with "sum(List<Integer> intList)".
How the community answered
(59 responses)- A71% (42)
- B5% (3)
- D8% (5)
- E15% (9)
Community Discussion
No community discussion yet for this question.