Learn AP Comp Sci

Problem of the Day

Friday, January 23, 2026


Problem:

Consider the following code segment, which successfully declares, initializes, and manipulates the ArrayList monsters.

ArrayList<String> monsters = new ArrayList<String>();
monsters.add("Godzilla");
monsters.add("Frankenstein");
monsters.add("The Thing");
monsters.add(2, "Dracula");
monsters.add("Mothra");
monsters.remove(1);
monsters.set(2, "Wolf Man");

What is the final state of the ArrayList, in order from lowest-index to highest-index?

  1. {"Godzilla", "Dracula", "Wolf Man", "Mothra"}
  2. {"Godzilla", "Frankenstein", "The Thing", "Mothra"}
  3. {"Godzilla", "Wolfman", "The Thing", "Mothra"}
  4. {"Frankenstein", "Dracula", "Wolfman"}

Show solution:

The correct answer is a. The state of the array after each instruction is shown here.

ArrayList<String> monsters = new ArrayList<String>(); →
{}
monsters.add("Godzilla"); →
{"Godzilla"}
monsters.add("Frankenstein"); →
{"Godzilla", "Frankenstein"}
monsters.add("The Thing"); →
{"Godzilla", "Frankenstein", "The Thing"}
monsters.add(2, "Dracula"); →
{"Godzilla", "Frankenstein", "Dracula", "The Thing"}
monsters.add("Mothra"); →
{"Godzilla", "Frankenstein", "Dracula", "The Thing", "Mothra"}
monsters.remove(1); →
{"Godzilla", "Dracula", "The Thing", "Mothra"}
monsters.set(2, "Wolf Man"); →
{"Godzilla", "Dracula", "Wolf Man", "Mothra"}