Objects: Instances of Classes

AP Computer Science A· difficulty 3/5

String[] names = new String[3];
System.out.println(names[0].length());

What happens when this code runs?

  • A

    NullPointerException because names[0] is null

    check_circle
  • B

    Prints 0

  • C

    Prints empty string

  • D

    Compile error

Explanation

A new String[] is filled with null references. Calling .length() on null throws NullPointerException.

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

Related questions