ArrayList Methods FRQ Practice

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

Question 1 of 5

A trail crew stores marker names.

(a) Write import java.util.ArrayList; and construct an empty ArrayList<String> named marks. (b) add "cairn" then add "fork". (c) State size(), the valid indices, and get(1). (d) Explain why indices start at 0.

Question 2 of 5

Complete the table for a weather-flag line. Then explain the insert shift and the set replacement.

After statement Index 0 Index 1 Index 2 size
add("mist") mist none none 1
add("gale") none
add(0, "calm")
set(2, "gust")

Question 3 of 5

Trace set and remove on a produce crate.

import java.util.ArrayList;
ArrayList<String> crate = new ArrayList<String>();
crate.add("pea");
crate.add("yam");
crate.add("fig");
String old = crate.set(0, "rye");
String gone = crate.remove(2);

(a) State old, gone, the remaining elements in order, and size. (b) Explain why set does not shift later elements. (c) Show the list just before remove(2) and explain that remove.

Question 4 of 5

Trace a heavy insert and two removals. Show the list after each of the last three statements.

import java.util.ArrayList;
ArrayList<String> row = new ArrayList<String>();
row.add("A");
row.add("B");
row.add("C");
row.add("D");
row.add(2, "X");
row.remove(1);
row.remove(2);

(a) Write the list contents after add(2, "X"), after remove(1), and after remove(2). (b) State final get(0), get(1), and size. (c) Explain each shift.

Question 5 of 5

A weigh station stores Integer tickets with autoboxing.

(a) Construct ArrayList<Integer> tickets, add 7, add 4, then add(1, 9). (b) Write int n = tickets.get(2); and state n and size. (c) Explain autoboxing on the add calls and unboxing into n. (d) State the valid indices of tickets afterward.