Answer is B.
Here's an example:
public static void main(String[] args) {
List <String> colors = new ArrayList();
colors.add("Green");
colors.add("Blue");
colors.add("Red");
colors.add("Yelow");
colors.remove(2);
colors.add(3,"Cyan");
System.out.println(colors);
}
The correct answer is the letter B, in line 18 the element that is in index 2 of the list is removed, remembering that Java indexes from 0, the element that will be removed is "red" and "yellow" which was in position 3 of the list after removal goes to position 2, in line 19 the element "cyan" is added at position 3 which is empty. and when printing on line 20, it returns [green, blue, yellow, cyan] which is the answer for the letter B. There is no way to throw the exception "IndexOutOfBounds" because in the line that the exclusion is made, it was made in a position that it had in the list and addition as well, since it was an empty position and the list was not set a fixed size for it.
Result should be B, from the documentation the add(int index, object element) method will only raise IndexOutOfBoundsException if the index is out of range (index < 0 || index > size()). https://docs.oracle.com/javase/8/docs/api/java/util/List.html#add-int-E-
upvoted 1 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
Priyamed
5 months, 3 weeks agoakbiyik
2 years, 2 months agocarloswork
2 years, 3 months agoiSnover
2 years, 3 months agoalex_au
2 years, 4 months ago