Practice mode Java

What happens when the Java code Object o = new String; o = 1; is run?

Correct answer C. An ArrayStoreException is thrown

Explanation

Arrays are covariant, so a String[] may be held in an Object[] variable, and storing an Integer type-checks at compile time. Each array remembers its real component type, however, and the JVM checks every store at run time. Putting an Integer into a String[] therefore raises ArrayStoreException, not the more familiar ClassCastException.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.