← Quiz: static, final and Nested Classes: all 10 questions
Quiz: static, final and Nested Classes · Question 3 of 10
What does this print? (code)
What does this print?
import java.util.*;
public class Main {
public static void main(String[] args) {
final List<Integer> list = new ArrayList<>();
list.add(5);
list.add(7);
System.out.println(list.size());
}
}
- A.Compile error: list is final
- B.0
- C.2
- D.Runtime exception
Answer & explanation
Correct answer: 2
final only stops list from pointing to another object. Adding elements to the same list is allowed.
Can you get the rest right without peeking?
Take the timed Quiz: static, final and Nested Classes quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →