Array Creation and Access

AP Computer Science A· difficulty 2/5

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

What happens?

  • A

    Compilation error

    check_circle
  • B

    Prints 3

  • C

    Prints 0

  • D

    ArrayIndexOutOfBoundsException

Explanation

For arrays, length is a public field, not a method. Writing a.length() (with parentheses) is a compile-time error. Use a.length without parentheses. (Strings use .length(), but arrays use .length.)

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

Related questions