What is the output of the following code? List<Integer> list = List.of(0, 1, 2, 3); System.out.println(list.indexOf(0) + " " + list.lastIndexOf(3));
Correct: indexOf(0) returns 0 and lastIndexOf(3) returns 3.
Why this answer
The list is [0, 1, 2, 3]. The indexOf(0) returns 0 because 0 is at index 0. The lastIndexOf(3) returns 3 because 3 is at index 3.
The output is "0 3".
Exam trap
Candidates might mistakenly think that lastIndexOf(3) returns 2 due to off-by-one error, or confuse with other list methods like get().
How to eliminate wrong answers
Option A is wrong because it assumes the removal succeeds and prints the remaining elements after removing evens, but the code throws an exception before any removal occurs. Option C is wrong because it incorrectly suggests the output is `1 2`, which would only happen if the removal succeeded and the list was modifiable, but the exception prevents that. Option D is wrong because it implies the output is `1 3`, which would be the result of a successful removal of evens from a modifiable list, but the immutable list causes an exception instead.