Skip to content
Sharp

Core Java · interview question

In the following code, which exception will be thrown if 'array' is null?

Multiple choiceHardchapter-6-curatedpremiumcore-javacode-reading-with-exceptions
java
public class Product {
    public static void main(String[] args) {
        int[] array = null;
        System.out.println(array[0]);
    }
}
Choose one answer
Answer and explanation

Correct answer

B. NullPointerException

Why

Attempting to access an element of a null array results in a NullPointerException because you are trying to dereference a null reference.

Test yourself on Core Java

A free test with instant, explained results. No account needed.

Start free test
All Core Java questions →