ArrayStoreException in Java Array Assignment
Short answer
A The program throws an ArrayStoreException at runtime because C is not assignable to the underlying B array.
Question
- 
Key Takeaways
The code compiles due to covariant array types but fails at runtime because the array's component type is B, not A or C.
This question tests understanding of Java array covariance and runtime type safety, demonstrating that assigning an incompatible object to a covariant array throws an exception.
Common mistake: Many learners choose D (fails to compile) because they confuse compile-time polymorphism with runtime array storage constraints.
Expert Analysis
Why the Answer Is Correct
The variable values is declared as type A[], which allows it to reference arrays of any subtype of A. However, it is instantiated as new B[2], meaning the actual runtime object is an array of type B[]. When values[0] = new C is executed, the JVM checks if C is assignable to B. Since C is not a subclass of B, the JVM throws an ArrayStoreException at runtime.
Why the Other Options Are Wrong
Option B and C are incorrect because the program does not print anything; it crashes before reaching any print statements. Option D is incorrect because the code compiles successfully. The assignment new C to an A[] reference is valid at compile time due to inheritance, even though it is invalid for the specific runtime array type B[].
Community Comment Notes
Community members correctly identify that ArrayStoreException is thrown because the array was created as B[]. One user noted that while A[] can hold B objects, it cannot hold C objects if the underlying array is specifically typed as B[]. Another comment clarified that if the array were created as new A[2], the assignment would have succeeded.
Exam Strategy
Remember that Java arrays are covariant: an array of subtypes can be assigned to a reference of a supertype. However, the runtime check ensures that only elements compatible with the actual array's component type can be stored.
Frequently Asked Questions
Why doesn't the compiler catch this error?
values is A[]. Since C extends A, the assignment is legal at compile time. Runtime checks enforce the actual array type.How to fix the code to avoid the exception?
new A[2] so the array can hold any subtype of A, including both B and C.Community Votes
100% of anonymous learners picked answer A. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 3 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 3
-
ASPushkin 👍 1answer: A A[] values = new B[2]; 5.1.6. Narrowing Reference Conversion From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a narrowing reference conversion from SC to TC. So that's ok because B extends A. values[0] = new C(); [2] The compiler guarantees that when you take an element from an array, it will be representative of the element type of the array itself. It doesn't matter what type the variable stores it. Values is the array of type B. That's way there is an ArrayStoreException.
-
d7bb0b2 👍 1 Answered AA[] a= new B[2]; => an array a that is reference to B array B is a subclass of A=> that is valid a[0] = new C()-> thown exception because real object o a is b type, and C is not subtype of B. IF A[] a = new A[2]; => then compile ok beacuse b and c are subclass of A
-
d7bb0b2 👍 1A is correct throw ArrayStoreException, because its declare to store new B[2], can contains only B elements oa public discussion thread on this question his subclasess, so C is not subclase of B cannot asing
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.