Developing Algorithms Using Arrays

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);
sts[1] = new Student("B", 80);
// sts[2] left as default
System.out.println(sts[2]);

What is printed?

  • A

    null

    check_circle
  • B

    B

  • C

    A

  • D

    NullPointerException

Explanation

Object arrays default to null. sts[2] was never assigned, so its value is null. Printing it just prints "null".

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

Related questions