🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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 = ...?

  1. A.ArrayList cannot be used as a variable type
  2. B.It makes the list faster
  3. C.The rest of the code depends only on List, so you can switch to another implementation by changing one line
  4. 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 →