Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard
Question 38
import java.util.ArrayList;
import java.util.List;
public class ListTest {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add(0, "Array");
list.add(0, "List");
System.out.println(list);
}
}
What will be the result of compiling and executing the ListTest class?
-
A
An exception is thrown at runtime
-
B
[Array]
-
C
[List, Array]
-
D
[Array, List]
-
E
[List]
Reveal correct answer
Correct answer: C
Explanation
list.add(0, "Array"); means list --> [Array]. list.add(0, "List"); means inserting "List" at the 0th index and shifting "Array" to the right. So after this operation, list --> [List, Array]. In the console, [List, Array] is printed.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
