Array Creation and Access

AP Computer Science A· difficulty 4/5

class Student {
  String name; int score;
  Student(String n, int s) { name = n; score = s; }
}
Student[] sts = new Student[3];
sts[0] = new Student("A", 90);
System.out.println(sts[1].name);

What happens?

  • A

    ArrayIndexOutOfBoundsException

  • B

    Prints null

  • C

    Prints A

  • D

    NullPointerException

    check_circle

Explanation

sts[1] is null (default for object array). Accessing .name on null throws a NullPointerException.

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

Related questions