Implementing ArrayList Algorithms MCQ Practice

Question 1 of 10

Lumen Workshop finds the lantern with the most oil using getters. What text does this Java code display?

public class Lantern {
    private int oil;
    public Lantern(int oil) {
        this.oil = oil;
    }
    public int getOil() {
        return oil;
    }
}

ArrayList<Lantern> row = new ArrayList<Lantern>();
row.add(new Lantern(6));
row.add(new Lantern(11));
row.add(new Lantern(9));
int max = row.get(0).getOil();
for (int i = 1; i < row.size(); i++) {
    if (row.get(i).getOil() > max) {
        max = row.get(i).getOil();
    }
}
System.out.print(max);
  1. A. 6
  2. B. 11
  3. C. 9
  4. D. 26

Question 2 of 10

Tide tickets average wait minutes. What text does this Java code display?

ArrayList<Integer> waits = new ArrayList<Integer>();
waits.add(5);
waits.add(10);
waits.add(6);
int sum = 0;
for (int w : waits) {
    sum += w;
}
System.out.print(sum / waits.size());
  1. A. 21
  2. B. 7
  3. C. 6
  4. D. 10

Question 3 of 10

Ridge Lodge checks whether at least one bunk is labeled loft. What text does this Java code display?

ArrayList<String> bunks = new ArrayList<String>();
bunks.add("nook");
bunks.add("loft");
bunks.add("nook");
boolean found = false;
for (int i = 0; i < bunks.size(); i++) {
    if (bunks.get(i).equals("loft")) {
        found = true;
    }
}
System.out.print(found);
  1. A. false
  2. B. true
  3. C. loft
  4. D. 1

Question 4 of 10

Peg heights must all be at least 12. What text does this Java code display?

ArrayList<Integer> pegs = new ArrayList<Integer>();
pegs.add(12);
pegs.add(15);
pegs.add(12);
boolean all = true;
for (int h : pegs) {
    if (h < 12) {
        all = false;
    }
}
System.out.print(all);
  1. A. false
  2. B. true
  3. C. 12
  4. D. 3

Question 5 of 10

A clerk counts how many show tickets are in row 4. What text does this Java code display?

public class Ticket {
    private int row;
    public Ticket(int row) {
        this.row = row;
    }
    public int getRow() {
        return row;
    }
}

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

Question 6 of 10

Stamp towns are compared with the next town. What text does this Java code display?

ArrayList<String> towns = new ArrayList<String>();
towns.add("Ayr");
towns.add("Ayr");
towns.add("Oban");
towns.add("Oban");
int same = 0;
for (int i = 0; i < towns.size() - 1; i++) {
    if (towns.get(i).equals(towns.get(i + 1))) {
        same++;
    }
}
System.out.print(same);
  1. A. 1
  2. B. 2
  3. C. 0
  4. D. 4

Question 7 of 10

A sorted wait list inserts 15. What text does this Java code display?

ArrayList<Integer> waits = new ArrayList<Integer>();
waits.add(8);
waits.add(12);
waits.add(21);
int value = 15;
int i = 0;
while (i < waits.size() && waits.get(i) < value) {
    i++;
}
waits.add(i, value);
System.out.print(waits);
  1. A. [8, 12, 21, 15]
  2. B. [15, 8, 12, 21]
  3. C. [8, 12, 15, 21]
  4. D. [8, 15, 12, 21]

Question 8 of 10

Hut codes shift one slot to the right. What text does this Java code display?

ArrayList<String> huts = new ArrayList<String>();
huts.add("elm");
huts.add("fir");
huts.add("oak");
String last = huts.get(huts.size() - 1);
for (int i = huts.size() - 1; i > 0; i--) {
    huts.set(i, huts.get(i - 1));
}
huts.set(0, last);
System.out.print(huts);
  1. A. [fir, oak, elm]
  2. B. [oak, elm, fir]
  3. C. [elm, fir, oak]
  4. D. [elm, elm, fir]

Question 9 of 10

A name list and a parallel int array of scores are traversed together. What text does this Java code display?

ArrayList<String> names = new ArrayList<String>();
names.add("Ada");
names.add("Lin");
names.add("Ada");
int[] score = {5, 8, 3};
int sum = 0;
for (int i = 0; i < names.size(); i++) {
    if (names.get(i).equals("Ada")) {
        sum += score[i];
    }
}
System.out.print(sum);
  1. A. 8
  2. B. 16
  3. C. 5
  4. D. 13

Question 10 of 10

Quiet stamps are deleted without decreasing i. What text does this Java code display?

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