Searching Algorithms MCQ Practice

Question 1 of 10

A rink locker linear-searches a 1D array from index 0 for badge 6. What text does this Java code display?

int[] badges = {9, 6, 2, 6};
int target = 6;
int checks = 0;
int found = -1;
for (int i = 0; i < badges.length; i++) {
    checks++;
    if (badges[i] == target) {
        found = i;
        break;
    }
}
System.out.print(found + " " + checks);
  1. A. 1 2
  2. B. 3 4
  3. C. 0 1
  4. D. 1 4

Question 2 of 10

A barge list linear-searches a 1D array for a missing hatch code. What text does this Java code display?

int[] hatches = {14, 21, 18, 7, 3};
int target = 10;
int checks = 0;
int found = -1;
for (int i = 0; i < hatches.length; i++) {
    checks++;
    if (hatches[i] == target) {
        found = i;
        break;
    }
}
System.out.print(found + " " + checks);
  1. A. -1 5
  2. B. -1 1
  3. C. 4 5
  4. D. 0 1

Question 3 of 10

A spool rack linear-searches from the last index of a 1D array. What text does this Java code display?

int[] spools = {4, 11, 8, 11};
int target = 11;
int checks = 0;
int found = -1;
for (int i = spools.length - 1; i >= 0; i--) {
    checks++;
    if (spools[i] == target) {
        found = i;
        break;
    }
}
System.out.print(found + " " + checks);
  1. A. 1 3
  2. B. 3 1
  3. C. 1 2
  4. D. 3 4

Question 4 of 10

A pantry list stores jar IDs in an ArrayList and searches from index 0. What text does this Java code display?

ArrayList<Integer> jars = new ArrayList<Integer>();
jars.add(20);
jars.add(15);
jars.add(20);
jars.add(9);
int target = 20;
int checks = 0;
int found = -1;
for (int i = 0; i < jars.size(); i++) {
    checks++;
    if (jars.get(i) == target) {
        found = i;
        break;
    }
}
System.out.print(found + " " + checks);
  1. A. 0 1
  2. B. 2 3
  3. C. 0 4
  4. D. 3 4

Question 5 of 10

A trail log linear-searches an ArrayList from index 0 for a missing marker. What text does this Java code display?

ArrayList<Integer> marks = new ArrayList<Integer>();
marks.add(5);
marks.add(2);
marks.add(8);
int target = 7;
int checks = 0;
int found = -1;
for (int i = 0; i < marks.size(); i++) {
    checks++;
    if (marks.get(i) == target) {
        found = i;
        break;
    }
}
System.out.print(found + " " + checks);
  1. A. -1 3
  2. B. 1 2
  3. C. -1 0
  4. D. 2 3

Question 6 of 10

A seed box is searched row by row, left to right in each row, stopping at the first match. What text does this Java code display?

int[][] box = {
    {9, 4},
    {6, 4}
};
int target = 9;
int checks = 0;
int foundR = -1;
int foundC = -1;
boolean found = false;
for (int r = 0; r < box.length; r++) {
    for (int c = 0; c < box[0].length; c++) {
        checks++;
        if (box[r][c] == target) {
            foundR = r;
            foundC = c;
            found = true;
            break;
        }
    }
    if (found) {
        break;
    }
}
System.out.print(foundR + "," + foundC + " " + checks);
  1. A. 0,0 1
  2. B. 0,0 4
  3. C. 1,1 4
  4. D. 0,1 2

Question 7 of 10

The seed-box search must finish row 0 before it can find a later match. What text does this Java code display?

int[][] box = {
    {9, 4},
    {6, 4}
};
int target = 6;
int checks = 0;
int foundR = -1;
int foundC = -1;
boolean found = false;
for (int r = 0; r < box.length; r++) {
    for (int c = 0; c < box[0].length; c++) {
        checks++;
        if (box[r][c] == target) {
            foundR = r;
            foundC = c;
            found = true;
            break;
        }
    }
    if (found) {
        break;
    }
}
System.out.print(foundR + "," + foundC + " " + checks);
  1. A. 1,0 3
  2. B. 1,0 1
  3. C. 0,0 1
  4. D. 1,1 4

Question 8 of 10

A window grid is searched row-wise for a missing pane code. What text does this Java code display?

int[][] panes = {
    {2, 5, 1},
    {8, 0, 3}
};
int target = 7;
int checks = 0;
boolean found = false;
for (int r = 0; r < panes.length; r++) {
    for (int c = 0; c < panes[0].length; c++) {
        checks++;
        if (panes[r][c] == target) {
            found = true;
            break;
        }
    }
    if (found) {
        break;
    }
}
System.out.print(found + " " + checks);
  1. A. false 6
  2. B. true 6
  3. C. false 3
  4. D. false 2

Question 9 of 10

A clerk compares two versions of a 2D row-wise search for target 5 in this grid.

int[][] grid = {
    {4, 5},
    {5, 1}
};

Version X breaks both loops at the first match. Version Y breaks only the inner loop, then continues the outer loop, overwriting the saved location whenever another match appears. Which statement is true?

  1. A. Both versions report row 0 column 1 after 2 comparisons
  2. B. X reports 0,1 after 2 comparisons; Y finishes with 1,0 after 3 comparisons
  3. C. Both versions report row 1 column 0 after 3 comparisons
  4. D. Y reports 0,1 after 2 comparisons; X reports 1,0 after 3 comparisons

Question 10 of 10

An ArrayList linear search may start at either end. The list stores 8, 3, 8, 1. Search L starts at index 0. Search R starts at the last index. Both look for 8 and stop at the first match. Which pair of results is correct?

  1. A. L returns 0 after 1 comparison; R returns 2 after 2 comparisons
  2. B. L returns 2 after 3 comparisons; R returns 0 after 4 comparisons
  3. C. Both return 0 after 1 comparison
  4. D. Both return 2 after 2 comparisons