← Quiz: Java Collections (List and Set): all 10 questions
Quiz: Java Collections (List and Set) · Question 8 of 10
Why write List<String names = new ArrayList<(); instead of ArrayList<String names = ...?
Why write List<String> names = new ArrayList<>(); instead of ArrayList<String> names = ...?
- A.ArrayList cannot be used as a variable type
- B.It makes the list faster
- C.The rest of the code depends only on List, so you can switch to another implementation by changing one line
- D.It allows the list to store int values
Answer & explanation
Correct answer: The rest of the code depends only on List, so you can switch to another implementation by changing one line
Programming to the interface keeps code flexible; only the creation line names the concrete class.
Can you get the rest right without peeking?
Take the timed Quiz: Java Collections (List and Set) quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →