ArrayList Methods MCQ Practice

Question 1 of 10

A ridge trail stores its first marker. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> path = new ArrayList<String>();
path.add("ridge");
System.out.print(path.get(0));
  1. A. 1
  2. B. 0
  3. C. ridge
  4. D. a compile-time error

Question 2 of 10

A mill list allows repeated names. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> mill = new ArrayList<String>();
mill.add("oak");
mill.add("pine");
mill.add("oak");
System.out.print(mill.size());
  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 0

Question 3 of 10

A mill inserts at index equal to the current size. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> mill = new ArrayList<String>();
mill.add("oak");
mill.add(1, "pine");
System.out.print(mill.get(0) + mill.get(1) + mill.size());
  1. A. pineoak2
  2. B. oakpine1
  3. C. pineoak1
  4. D. oakpine2

Question 4 of 10

A signal mast replaces the last flag. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> flags = new ArrayList<String>();
flags.add("red");
flags.add("blue");
flags.add("gold");
flags.set(2, "teal");
System.out.print(flags.get(0) + "," + flags.get(2));
  1. A. red,gold
  2. B. red,teal
  3. C. teal,blue
  4. D. red,blue

Question 5 of 10

A signal mast prints the previous value from set, then the new occupant. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> flags = new ArrayList<String>();
flags.add("red");
flags.add("blue");
String prev = flags.set(1, "gray");
System.out.print(prev + flags.get(1));
  1. A. graygray
  2. B. redgray
  3. C. bluegray
  4. D. blueblue

Question 6 of 10

A quay list removes its last net. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> quay = new ArrayList<String>();
quay.add("net");
quay.add("cork");
quay.add("hook");
quay.remove(2);
System.out.print(quay.get(1) + quay.size());
  1. A. cork2
  2. B. hook2
  3. C. cork3
  4. D. net2

Question 7 of 10

A quay list prints the removed front element and the new front. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> quay = new ArrayList<String>();
quay.add("net");
quay.add("cork");
quay.add("hook");
String pulled = quay.remove(0);
System.out.print(pulled + "," + quay.get(0));
  1. A. cork,net
  2. B. net,cork
  3. C. net,hook
  4. D. hook,cork

Question 8 of 10

A loot sack inserts, removes, then appends. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> loot = new ArrayList<String>();
loot.add("map");
loot.add("key");
loot.add(1, "gem");
loot.remove(2);
loot.add("orb");
System.out.print(loot.get(0) + loot.get(1) + loot.get(2) + loot.size());
  1. A. mapkeyorb3
  2. B. mapgemkey3
  3. C. gemmaporb3
  4. D. mapgemorb3

Question 9 of 10

A bolt list uses set's returned Integer, then inserts at 0. What text does this Java code display?

import java.util.ArrayList;
ArrayList<Integer> bolts = new ArrayList<Integer>();
bolts.add(3);
bolts.add(8);
int old = bolts.set(1, 5);
bolts.add(0, 1);
System.out.print(old + "," + bolts.get(2) + "," + bolts.size());
  1. A. 5,8,3
  2. B. 8,3,3
  3. C. 8,5,3
  4. D. 8,5,2

Question 10 of 10

A catalog compares list constructions. Using the table, which statement is true?

Code Notes
ArrayList<String> a = new ArrayList<String>(); generic, empty
ArrayList<Integer> b = new ArrayList<Integer>(); stores Integer references
ArrayList<int> c = new ArrayList<int>(); not valid Java
ArrayList d = new ArrayList(); raw type, not preferred
  1. A. ArrayList<int> compiles because int is numeric
  2. B. Generic ArrayList<E> is preferred, and ArrayList cannot store primitive int values
  3. C. A raw ArrayList is required in the AP CSA subset
  4. D. An empty ArrayList has size 1 until the first get