ProgrammingHardWipro · Cognizant pattern

What happens when this line is compiled?

What happens when this line is compiled?
List<String>[] arr = new List<String>[10];
  1. A. It compiles and creates an array of 10 empty String lists
  2. B. Compile-time error: generic array creation is not allowed
  3. C. It compiles but throws ArrayStoreException on first write
  4. D. It compiles with an unchecked warning only

Correct answer: B. Compile-time error: generic array creation is not allowed

Java forbids creating an array of a parameterized type directly, because arrays are reified (they remember their element type at run time) while generics are erased; allowing new List<String>[10] would let code bypass generics' type safety through an array store. It is a hard compile-time error, not just a warning.

More OOP with C++ and Java questions

Exams that ask this topic