Searching Algorithms MCQ Practice

Question 1 of 10

A ferry ticket bar stores lane numbers in a 1D array and linear-searches from index 0. Each comparison tests equality with the target. What text does this Java code display?

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

Question 2 of 10

A kiln shelf linear-searches a 1D array for a missing glaze code. What text does this Java code display?

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

Question 3 of 10

A lantern hook linear-searches from the right end of a 1D array. What text does this Java code display?

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

Question 4 of 10

A booth ledger stores bin codes in an ArrayList and linear-searches from index 0. What text does this Java code display?

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

Question 5 of 10

A quay notebook linear-searches an ArrayList for a missing pass number. What text does this Java code display?

ArrayList<Integer> passes = new ArrayList<Integer>();
passes.add(3);
passes.add(8);
passes.add(6);
passes.add(1);
int target = 9;
int checks = 0;
boolean found = false;
for (int i = 0; i < passes.size(); i++) {
    checks++;
    if (passes.get(i) == target) {
        found = true;
        break;
    }
}
System.out.print(found + " " + checks);
  1. A. true 4
  2. B. false 4
  3. C. false 1
  4. D. true 0

Question 6 of 10

A muffin tin is searched row by row: each row is linear-searched from column 0. The search stops when the target is found. What text does this Java code display?

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

Question 7 of 10

The same muffin-tin search continues into later rows when the target is not in row 0. What text does this Java code display?

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

Question 8 of 10

A crate wall linear-searches every row in order for a missing stamp. What text does this Java code display?

int[][] wall = {
    {3, 6},
    {8, 2},
    {5, 7}
};
int target = 4;
int checks = 0;
boolean found = false;
for (int r = 0; r < wall.length; r++) {
    for (int c = 0; c < wall[0].length; c++) {
        checks++;
        if (wall[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 2
  4. D. false 3

Question 9 of 10

A clerk traces a row-wise linear search. Complete the missing comparison count for target 9.

Visit order Row Col Value Equal to 9?
1 0 0 3 no
2 0 1 8 no
3 0 2 1 no
4 1 0 4 no
5 1 1 9 yes

If the nested loops break after this match, how many equality comparisons run?

  1. A. 4
  2. B. 5
  3. C. 6
  4. D. 2

Question 10 of 10

Two linear searches look for 7 in the same 1D array. Search P starts at index 0. Search Q starts at the last index. Each stops at the first match it finds. Which statement is true?

int[] arr = {7, 4, 7, 2};
  1. A. Both searches return index 0
  2. B. P returns 0 and Q returns 2
  3. C. Both searches return index 2
  4. D. P returns 2 and Q returns 0