Oracle
1Z0-803 · Question #98
Given the code fragment: 1. ArrayList<Integer> list = new ArrayList<>(1); 2. list.add(1001); 3. list.add(1002); 4. System.out.println(list.get(list.size())); What is the result?
The correct answer is C. An exception is thrown at run time due to error on line 4. The code compiles fine. At runtime an IndexOutOfBoundsException is thrown when the second list item is added.
Working with Selected Classes from the Java API
Question
Given the code fragment: 1. ArrayList<Integer> list = new ArrayList<>(1); 2. list.add(1001); 3. list.add(1002); 4. System.out.println(list.get(list.size())); What is the result?
Options
- ACompilation fails due to an error on line 1.
- BAn exception is thrown at run time due to error on line 3
- CAn exception is thrown at run time due to error on line 4
- D1002
How the community answered
(51 responses)- A16% (8)
- B4% (2)
- C73% (37)
- D8% (4)
Explanation
The code compiles fine. At runtime an IndexOutOfBoundsException is thrown when the second list item is added.
Topics
#ArrayList#IndexOutOfBoundsException#Java Collections#runtime errors
Community Discussion
No community discussion yet for this question.