Array Creation and Access

AP Computer Science A· difficulty 2/5

int[] a = {1, 2, 3};
System.out.println(a[3]);

What happens when this code runs?

  • A

    ArrayIndexOutOfBoundsException is thrown

    check_circle
  • B

    It prints 3

  • C

    It prints 0

  • D

    NullPointerException is thrown

Explanation

Valid indices for an array of length 3 are 0, 1, and 2. Accessing a[3] throws ArrayIndexOutOfBoundsException at runtime.

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

Related questions