ArrayList Methods MCQ Practice

Question 1 of 10

A booth starts an empty prize-tag list. What text does this Java code display?

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

Question 2 of 10

A ferry appends two stop names. What text does this Java code display?

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

Question 3 of 10

A yarn shop inserts a color at index 1. What text does this Java code display?

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

Question 4 of 10

A tray rack replaces the middle wood name. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> trays = new ArrayList<String>();
trays.add("oak");
trays.add("elm");
trays.add("ash");
trays.set(1, "fir");
System.out.print(trays.get(1) + trays.size());
  1. A. elm3
  2. B. fir2
  3. C. oak3
  4. D. fir3

Question 5 of 10

A tray rack captures the value returned by set. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> trays = new ArrayList<String>();
trays.add("oak");
trays.add("elm");
String old = trays.set(0, "yew");
System.out.print(old);
  1. A. yew
  2. B. oak
  3. C. elm
  4. D. 0

Question 6 of 10

A dock list removes the element at index 0. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> dock = new ArrayList<String>();
dock.add("kelp");
dock.add("buoy");
dock.add("rope");
dock.remove(0);
System.out.print(dock.get(0) + "," + dock.size());
  1. A. buoy,2
  2. B. kelp,2
  3. C. kelp,3
  4. D. buoy,3

Question 7 of 10

A dock list stores the String returned by remove. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> dock = new ArrayList<String>();
dock.add("kelp");
dock.add("buoy");
String gone = dock.remove(1);
System.out.print(gone + dock.size());
  1. A. kelp1
  2. B. buoy2
  3. C. buoy1
  4. D. kelp2

Question 8 of 10

A pantry list mixes insert, replace, and remove. What text does this Java code display?

import java.util.ArrayList;
ArrayList<String> bins = new ArrayList<String>();
bins.add("tea");
bins.add("fig");
bins.add(0, "rye");
bins.set(2, "nut");
bins.remove(1);
System.out.print(bins.get(0) + "," + bins.get(1) + "," + bins.size());
  1. A. tea,fig,2
  2. B. rye,tea,2
  3. C. rye,nut,3
  4. D. rye,nut,2

Question 9 of 10

A score list autoboxes ints, inserts, then removes. What text does this Java code display?

import java.util.ArrayList;
ArrayList<Integer> scores = new ArrayList<Integer>();
scores.add(4);
scores.add(9);
scores.add(1, 6);
scores.remove(0);
System.out.print(scores.get(0) + scores.get(1));
  1. A. 13
  2. B. 15
  3. C. 69
  4. D. 10

Question 10 of 10

A clerk tracks valid indices while a list grows. Using the table, which statement is true?

After size Valid indices
ArrayList<String> a = new ArrayList<String>(); 0 none
a.add("mist"); 1 0
a.add("gale"); 2 0 and 1
  1. A. After two add calls, get(0) and get(1) are valid, but get(2) is not
  2. B. Valid indices always start at 1
  3. C. size is 2 so get(2) is valid
  4. D. An empty list has valid index 0