ArrayList Traversals FRQ Practice

Responses receive AI feedback against a rubric after submission. Scores are practice estimates.

Question 1 of 5

Complete the missing cells for this Wickmill ribbon purge. Then explain why one short ribbon remains.

ArrayList<Integer> lens = new ArrayList<Integer>();
lens.add(12);
lens.add(4);
lens.add(4);
lens.add(9);
for (int i = 0; i < lens.size(); i++) {
    if (lens.get(i) < 5) {
        lens.remove(i);
    }
}
After statement i lens
i is 0 and 12 is kept 0 [12, 4, 4, 9]
first 4 is removed
loop i++ runs after that remove
loop ends

Question 2 of 5

Cinder Dock stores boat names in an ArrayList. Write printDock so that printDock(boats) displays each name with no extra spaces or line breaks, in stored order. Use an enhanced for loop. Do not add or remove elements.

public void printDock(ArrayList<String> boats) {
    /* write this method */
}

Question 3 of 5

A prize booth tries to drop expired tickets.

public void dropExpired(ArrayList<String> tickets) {
    for (String t : tickets) {
        if (t.equals("old")) {
            tickets.remove(0);
        }
    }
}

(a) Name the exception this method can throw and why the enhanced for loop causes it. (b) Rewrite dropExpired using an indexed for loop that removes every "old" without skipping the next ticket. (c) State one legal index range for get and remove on tickets.

Question 4 of 5

Write removeQuiet so every lantern with oil less than 3 is deleted from lamps. Traverse with indexes from the back of the list so a remove cannot skip a neighbor. Lantern has getOil() as shown. Do not use an enhanced for loop.

public class Lantern {
    private int oil;
    public Lantern(int oil) {
        this.oil = oil;
    }
    public int getOil() {
        return oil;
    }
}

public void removeQuiet(ArrayList<Lantern> lamps) {
    /* write this method */
}

Question 5 of 5

A marsh boardwalk prints the last plank label.

ArrayList<String> planks = new ArrayList<String>();
planks.add("north");
planks.add("mid");
planks.add("south");
System.out.print(planks.get(planks.size()));

(a) Does this fragment throw an exception? Name it if so. (b) Write one statement that prints the last label when the list is not empty. (c) Write a loop condition that visits every index without going out of range.