ArrayList Traversals MCQ Practice

Question 1 of 10

Nettle Station stores kite colors. What text does this Java code display?

ArrayList<String> kites = new ArrayList<String>();
kites.add("jade");
kites.add("gold");
kites.add("ice");
for (String c : kites) {
    System.out.print(c);
}
  1. A. goldice
  2. B. jadegoldice
  3. C. jade gold ice
  4. D. ConcurrentModificationException

Question 2 of 10

A rink lists skate sizes starting at index 1. What text does this Java code display?

ArrayList<Integer> sizes = new ArrayList<Integer>();
sizes.add(8);
sizes.add(9);
sizes.add(7);
for (int i = 1; i < sizes.size(); i++) {
    System.out.print(sizes.get(i));
}
  1. A. 897
  2. B. 97
  3. C. 89
  4. D. IndexOutOfBoundsException

Question 3 of 10

An orchard clerk prints the next stamp while i still runs to size(). What happens when this Java code runs?

ArrayList<String> stamps = new ArrayList<String>();
stamps.add("pear");
stamps.add("plum");
stamps.add("fig");
for (int i = 0; i < stamps.size(); i++) {
    System.out.print(stamps.get(i + 1));
}
  1. A. It displays plumfig and then throws IndexOutOfBoundsException
  2. B. It displays plumfigpear
  3. C. It displays pearplumfig
  4. D. It throws ConcurrentModificationException before printing

Question 4 of 10

Quay Market deletes "wet" tags without adjusting i. What text does this Java code display?

ArrayList<String> tags = new ArrayList<String>();
tags.add("dry");
tags.add("wet");
tags.add("wet");
tags.add("dry");
for (int i = 0; i < tags.size(); i++) {
    if (tags.get(i).equals("wet")) {
        tags.remove(i);
    }
}
System.out.print(tags);
  1. A. [dry, dry]
  2. B. [dry, wet, dry]
  3. C. [dry, wet, wet, dry]
  4. D. ConcurrentModificationException

Question 5 of 10

The market deletes "wet" from the back of the list. What text does this Java code display?

ArrayList<String> tags = new ArrayList<String>();
tags.add("dry");
tags.add("wet");
tags.add("wet");
tags.add("dry");
for (int i = tags.size() - 1; i >= 0; i--) {
    if (tags.get(i).equals("wet")) {
        tags.remove(i);
    }
}
System.out.print(tags);
  1. A. [dry, wet, dry]
  2. B. [dry, dry]
  3. C. [wet, wet]
  4. D. IndexOutOfBoundsException

Question 6 of 10

A depot appends a copy of each car id inside an enhanced for loop. Which statement is true?

ArrayList<String> cars = new ArrayList<String>();
cars.add("N7");
cars.add("N9");
for (String id : cars) {
    cars.add(id + "x");
}
  1. A. cars becomes [N7, N9, N7x, N9x]
  2. B. The code throws ConcurrentModificationException
  3. C. The code throws IndexOutOfBoundsException
  4. D. cars becomes [N7x, N9x]

Question 7 of 10

A pottery kiln prints the last batch tag. What text does this Java code display?

ArrayList<String> batches = new ArrayList<String>();
batches.add("bisque");
batches.add("glaze");
batches.add("fire");
System.out.print(batches.get(batches.size() - 1));
  1. A. bisque
  2. B. fire
  3. C. IndexOutOfBoundsException
  4. D. glaze

Question 8 of 10

Library hold slips are printed two ways. Which statement is true about this Java code?

ArrayList<String> holds = new ArrayList<String>();
holds.add("Poe");
holds.add("Ng");
String a = "";
for (int i = 0; i < holds.size(); i++) {
    a += holds.get(i);
}
String b = "";
for (String h : holds) {
    b += h;
}
System.out.print(a.equals(b));
  1. A. false
  2. B. true
  3. C. PoeNg
  4. D. ConcurrentModificationException

Question 9 of 10

Marina slip codes are printed backward, but the loop stops at i > 0. The list is shown. What text does the code display?

Index 0 1 2
Code A3 B1 C8
for (int i = slips.size() - 1; i > 0; i--) {
    System.out.print(slips.get(i));
}
  1. A. C8B1A3
  2. B. C8B1
  3. C. B1A3
  4. D. IndexOutOfBoundsException

Question 10 of 10

Alpine hut stamps are emptied from the front with remove(0). What text does this Java code display?

ArrayList<String> stamps = new ArrayList<String>();
stamps.add("ibex");
stamps.add("fir");
stamps.add("col");
while (stamps.size() > 0) {
    System.out.print(stamps.remove(0));
}
  1. A. ibexfircol
  2. B. colfiribex
  3. C. ConcurrentModificationException
  4. D. IndexOutOfBoundsException