Array Creation and Access

AP Computer Science A· difficulty 2/5

int[] a;
a[0] = 1;

What is wrong?

  • A

    Arrays cannot be declared without size

  • B

    Index 0 is reserved

  • C

    Nothing is wrong

  • D

    a is declared but not instantiated; you must allocate with new int[size]

    check_circle

Explanation

Declaring int[] a only creates a reference variable. You must allocate the array with a = new int[n] before assigning elements.

Want 10 more like this — adaptive to your weak spots?

Related questions