ArrayList Traversals MCQ Practice

Question 1 of 10

Brine Harbor stores leftover mooring tags. What text does this Java code display?

ArrayList<String> tags = new ArrayList<String>();
tags.add("oak");
tags.add("elm");
tags.add("ash");
for (String t : tags) {
    System.out.print(t);
}
  1. A. oakelmash
  2. B. oak elm ash
  3. C. elmash
  4. D. IndexOutOfBoundsException

Question 2 of 10

The same harbor list is printed with indexes. What text does this Java code display?

ArrayList<String> tags = new ArrayList<String>();
tags.add("oak");
tags.add("elm");
tags.add("ash");
for (int i = 0; i < tags.size(); i++) {
    System.out.print(tags.get(i));
}
  1. A. ashelmoak
  2. B. oakelmash
  3. C. oakelm
  4. D. IndexOutOfBoundsException

Question 3 of 10

A cider mill lists crate counts and uses a loop that is one step too long. What happens when this Java code runs?

ArrayList<Integer> crates = new ArrayList<Integer>();
crates.add(4);
crates.add(9);
crates.add(1);
for (int i = 0; i <= crates.size(); i++) {
    System.out.print(crates.get(i));
}
  1. A. It displays 491 and then ends normally
  2. B. It displays nothing and throws ConcurrentModificationException
  3. C. It displays 491 and then throws IndexOutOfBoundsException
  4. D. It displays 49 and then ends normally

Question 4 of 10

A clerk tries to print the last ferry stub. What happens when this Java code runs?

ArrayList<String> stubs = new ArrayList<String>();
stubs.add("dawn");
stubs.add("noon");
stubs.add("dusk");
System.out.print(stubs.get(stubs.size()));
  1. A. dusk
  2. B. dawn
  3. C. IndexOutOfBoundsException
  4. D. ConcurrentModificationException

Question 5 of 10

Puddle Fair removes 3-point ring tosses but forgets to adjust the index. What text does this Java code display?

ArrayList<Integer> rings = new ArrayList<Integer>();
rings.add(6);
rings.add(3);
rings.add(3);
rings.add(8);
for (int i = 0; i < rings.size(); i++) {
    if (rings.get(i) == 3) {
        rings.remove(i);
    }
}
System.out.print(rings);
  1. A. [6, 8]
  2. B. [6, 3, 8]
  3. C. [6, 3, 3, 8]
  4. D. ConcurrentModificationException

Question 6 of 10

The fair tries again and decreases i after a remove. What text does this Java code display?

ArrayList<Integer> rings = new ArrayList<Integer>();
rings.add(6);
rings.add(3);
rings.add(3);
rings.add(8);
for (int i = 0; i < rings.size(); i++) {
    if (rings.get(i) == 3) {
        rings.remove(i);
        i--;
    }
}
System.out.print(rings);
  1. A. [6, 3, 8]
  2. B. [6, 8]
  3. C. [8]
  4. D. [6, 3, 3, 8]

Question 7 of 10

A quilt booth tries to delete during an enhanced for loop. What happens when this Java code runs?

ArrayList<String> squares = new ArrayList<String>();
squares.add("red");
squares.add("tan");
squares.add("tan");
for (String color : squares) {
    if (color.equals("tan")) {
        squares.remove(1);
    }
}
  1. A. squares becomes [red, tan]
  2. B. squares becomes [red]
  3. C. The code throws ConcurrentModificationException
  4. D. The code throws IndexOutOfBoundsException and squares is unchanged

Question 8 of 10

Observatory labels are printed from the end of the list. What text does this Java code display?

ArrayList<String> stars = new ArrayList<String>();
stars.add("vega");
stars.add("deneb");
stars.add("altair");
for (int i = stars.size() - 1; i >= 0; i--) {
    System.out.print(stars.get(i));
}
  1. A. vega deneb altair
  2. B. altairdenebvega
  3. C. denebaltair
  4. D. IndexOutOfBoundsException

Question 9 of 10

A bakery traces loaf IDs with the loop below. The starting list is shown. What text does the code display?

Index 0 1 2 3
Value rye oat fig rye
for (int i = 0; i < loaves.size() - 1; i++) {
    System.out.print(loaves.get(i));
}
  1. A. ryeoatfigrye
  2. B. ryeoatfig
  3. C. oatfigrye
  4. D. IndexOutOfBoundsException

Question 10 of 10

A canal log copies each mark by adding during an enhanced for loop. Which statement is true?

ArrayList<String> marks = new ArrayList<String>();
marks.add("L1");
marks.add("L2");
for (String m : marks) {
    marks.add(m);
}
  1. A. marks becomes [L1, L2, L1, L2] and the loop ends
  2. B. The loop runs forever because size keeps growing
  3. C. The code throws ConcurrentModificationException
  4. D. The code throws IndexOutOfBoundsException on the first add