Implementing ArrayList Algorithms MCQ Practice

Question 1 of 10

Cobble Orchard finds the coldest crate reading. What text does this Java code display?

ArrayList<Integer> temps = new ArrayList<Integer>();
temps.add(12);
temps.add(4);
temps.add(9);
temps.add(4);
int min = temps.get(0);
for (int i = 1; i < temps.size(); i++) {
    if (temps.get(i) < min) {
        min = temps.get(i);
    }
}
System.out.print(min);
  1. A. 12
  2. B. 4
  3. C. 9
  4. D. 29

Question 2 of 10

Maple Depot totals ticket points and then averages with int division. What text does this Java code display?

ArrayList<Integer> pts = new ArrayList<Integer>();
pts.add(3);
pts.add(12);
pts.add(8);
int sum = 0;
for (int i = 0; i < pts.size(); i++) {
    sum += pts.get(i);
}
System.out.print(sum / pts.size());
  1. A. 7
  2. B. 8
  3. C. 23
  4. D. 3

Question 3 of 10

A harbor checks whether at least one crate is overweight. What text does this Java code display?

ArrayList<Integer> kg = new ArrayList<Integer>();
kg.add(9);
kg.add(14);
kg.add(8);
boolean heavy = false;
for (int n : kg) {
    if (n > 10) {
        heavy = true;
    }
}
System.out.print(heavy);
  1. A. false
  2. B. true
  3. C. 14
  4. D. 2

Question 4 of 10

The harbor then checks whether every crate is overweight. What text does this Java code display?

ArrayList<Integer> kg = new ArrayList<Integer>();
kg.add(9);
kg.add(14);
kg.add(8);
boolean all = true;
for (int n : kg) {
    if (n <= 10) {
        all = false;
    }
}
System.out.print(all);
  1. A. true
  2. B. false
  3. C. 9
  4. D. 1

Question 5 of 10

A mill counts how many sacks are at least 20. What text does this Java code display?

ArrayList<Integer> sacks = new ArrayList<Integer>();
sacks.add(20);
sacks.add(11);
sacks.add(25);
sacks.add(20);
int count = 0;
for (int i = 0; i < sacks.size(); i++) {
    if (sacks.get(i) >= 20) {
        count++;
    }
}
System.out.print(count);
  1. A. 4
  2. B. 3
  3. C. 2
  4. D. 20

Question 6 of 10

Ribbon dyes are checked for consecutive duplicates. What text does this Java code display?

ArrayList<String> dyes = new ArrayList<String>();
dyes.add("red");
dyes.add("red");
dyes.add("blue");
dyes.add("blue");
dyes.add("blue");
int pairs = 0;
for (int i = 0; i < dyes.size() - 1; i++) {
    if (dyes.get(i).equals(dyes.get(i + 1))) {
        pairs++;
    }
}
System.out.print(pairs);
  1. A. 2
  2. B. 3
  3. C. 5
  4. D. IndexOutOfBoundsException

Question 7 of 10

Booth numbers are checked for any duplicate, not only neighbors. What text does this Java code display?

ArrayList<Integer> booths = new ArrayList<Integer>();
booths.add(4);
booths.add(9);
booths.add(4);
boolean dup = false;
for (int i = 0; i < booths.size(); i++) {
    for (int j = i + 1; j < booths.size(); j++) {
        if (booths.get(i).equals(booths.get(j))) {
            dup = true;
        }
    }
}
System.out.print(dup);
  1. A. false
  2. B. true
  3. C. 4
  4. D. 2

Question 8 of 10

Crate IDs rotate one slot to the left. What text does this Java code display?

ArrayList<Integer> ids = new ArrayList<Integer>();
ids.add(2);
ids.add(9);
ids.add(4);
ids.add(7);
int first = ids.get(0);
for (int i = 0; i < ids.size() - 1; i++) {
    ids.set(i, ids.get(i + 1));
}
ids.set(ids.size() - 1, first);
System.out.print(ids);
  1. A. [7, 2, 9, 4]
  2. B. [9, 4, 7, 2]
  3. C. [2, 9, 4, 7]
  4. D. [4, 7, 2, 9]

Question 9 of 10

Ferry names are reversed in place. What text does this Java code display?

ArrayList<String> boats = new ArrayList<String>();
boats.add("lea");
boats.add("nona");
boats.add("pia");
boats.add("tove");
for (int i = 0; i < boats.size() / 2; i++) {
    int j = boats.size() - 1 - i;
    String hold = boats.get(i);
    boats.set(i, boats.get(j));
    boats.set(j, hold);
}
System.out.print(boats);
  1. A. [lea, nona, pia, tove]
  2. B. [tove, pia, nona, lea]
  3. C. [nona, lea, tove, pia]
  4. D. [tove, lea, nona, pia]

Question 10 of 10

Even lantern hours are deleted. The loop adjusts i after each remove. What text does this Java code display?

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