AP CSA Unit-Level MCQ

Array Search

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

What array search covers

Linear search walks the array comparing each element to the target and returns as soon as it finds a match. Returning the index is more useful than returning true, and -1 is the standard signal that nothing was found because it is never a valid index. The search must return -1 after the loop finishes, not inside it, since a mismatch at one position says nothing about the rest.

Where students lose points

Returning false or -1 from inside the loop on the first non-match ends the search after a single comparison. Students also use == to compare Strings or objects instead of equals, which fails for values built at runtime, and they forget the fall-through return entirely, which does not compile.

How it shows up on the AP exam

Unit 4 content and a guaranteed presence on the exam. Search is also the base case that binary search and sorting questions build on, so the structure needs to be automatic.

For practice use only.

Array Search MCQ Practice

AP CSA array search practice: linear search structure, returning an index versus a boolean, early exit with return, and the -1 not-found convention.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Code Tracing 01 - Array Search: track the final printed value.

int[] data = {4, 9, 2, 7, 5, 1};
int target = 7;
int found = -1;
for (int i = 0; i < data.length; i++) {
  if (data[i] == target) {
    found = i;
    break;
  }
}
System.out.print(found);

All 15 array search questions

Work through the interactive quiz above first. This is the full array search question set with worked solutions, so students can review any question after attempting it.

  1. 1.Code Tracing 01 - Array Search: track the final printed value.

    int[] data = {4, 9, 2, 7, 5, 1};
    int target = 7;
    int found = -1;
    for (int i = 0; i < data.length; i++) {
      if (data[i] == target) {
        found = i;
        break;
      }
    }
    System.out.print(found);
    • 7
    • -1
    • 4
    • 3
    Show worked solution

    Correct answer: 3

    A linear search returns the position of the first match, and the break stops the scan as soon as it is found.

  2. 2.Code Tracing 12 - Array Search: follow the variable updates.

    int[] data = {6, 7, 4, 9, 7, 3};
    int target = 999;
    int found = -1;
    for (int i = 0; i < data.length; i++) {
      if (data[i] == target) found = i;
    }
    System.out.print(found);
    • -1
    • 0
    • 6
    • 999
    Show worked solution

    Correct answer: -1

    The sentinel value of -1 survives untouched when no element matches, which is how callers detect a failed search.

  3. 3.Code Tracing 28 - Array Search: trace the branch and loop path.

    int[] data = {9, 7, 2, 8, 10, 6};
    int target = 2;
    int first = -1;
    int last = -1;
    for (int i = 0; i < data.length; i++) {
      if (data[i] == target) {
        if (first == -1) first = i;
        last = i;
      }
    }
    System.out.print(first + ":" + last);
    • 2:2
    • -1:-1
    • The code does not compile.
    • A runtime exception is thrown.
    Show worked solution

    Correct answer: 2:2

    Guarding the first assignment keeps it pinned to the earliest match, while last is overwritten on every match.

  4. 4.Code Tracing 39 - Array Search: watch the index changes.

    int[] data = {11, 8, 4, 10, 6, 8};
    int count = 0;
    for (int v : data) {
      if (v > 8) count++;
    }
    System.out.print(count);
    • 3
    • 2
    • 4
    • 6
    Show worked solution

    Correct answer: 2

    Counting matches requires scanning the whole array, so no break appears here.

  5. 5.Code Tracing 50 - Array Search: evaluate the state change step by step.

    int[] data = {13, 9, 6, 8, 8, 10};
    int target = 9;
    int idx = 0;
    while (idx < data.length && data[idx] != target) {
      idx++;
    }
    System.out.print(idx < data.length ? idx : -1);
    • 2
    • 9
    • 1
    • -1
    Show worked solution

    Correct answer: 1

    Checking the bounds before the array access is what stops this loop from reading past the end when the target is missing.

  6. 6.What is printed by the following code segment?

    int[] a = {1, 3, 5, 7, 9};
    int lo = 0;
    int hi = a.length - 1;
    int c = 0;
    while (lo <= hi)
    {
        int mid = (lo + hi) / 2;
        c++;
        if (a[mid] == 7) { lo = hi + 1; }
        else if (a[mid] < 7) { lo = mid + 1; }
        else { hi = mid - 1; }
    }
    System.out.print(c);
    • 3
    • 1
    • 2
    • 5
    Show worked solution

    Correct answer: 2

    The midpoints examined are 5 and then 7, so two comparisons find the target.

  7. 7.What is printed by the following code segment?

    int[] a = {4, 4, 4};
    int first = -1;
    int last = -1;
    for (int i = 0; i < a.length; i++)
    {
        if (a[i] == 4)
        {
            if (first == -1) { first = i; }
            last = i;
        }
    }
    System.out.print(first + " " + last);
    • 0 0
    • 2 2
    • -1 -1
    • 0 2
    Show worked solution

    Correct answer: 0 2

    The first index is recorded only once while the last is overwritten on every match.

  8. 8.What is printed by the following code segment?

    int[] a = {2, 4, 6};
    int t = 5;
    int pos = -1;
    for (int i = 0; i < a.length && pos == -1; i++)
    {
        if (a[i] >= t) { pos = i; }
    }
    System.out.print(pos);
    • 2
    • -1
    • 1
    • 3
    Show worked solution

    Correct answer: 2

    The loop condition itself stops the search once a position is recorded, and 6 is the first value at least 5.

  9. 9.What is printed by the following code segment?

    int[] a = {5, 2, 8, 2};
    int c = 0;
    for (int i = 0; i < a.length; i++)
    {
        if (a[i] == 2) { c++; }
        else if (a[i] > 5) { c += 2; }
    }
    System.out.print(c);
    • 3
    • 4
    • 2
    • 5
    Show worked solution

    Correct answer: 4

    Two matches add one each and the single larger value adds two.

  10. 10.Why does binary search fail on the array below?

    int[] a = {9, 1, 7, 3, 5};
    • The array has an odd number of elements.
    • Binary search requires values greater than 5.
    • The array is not sorted, so discarding half the range can discard the target.
    • The array must contain no duplicates.
    Show worked solution

    Correct answer: The array is not sorted, so discarding half the range can discard the target.

    The halving decision is only meaningful when the data is ordered, and unsorted input produces a wrong answer rather than an error.

  11. 11.What is printed by the following code segment?

    int[] a = {1, 2, 3, 4, 5, 6, 7, 8};
    int lo = 0;
    int hi = a.length - 1;
    int mid = 0;
    while (lo <= hi)
    {
        mid = (lo + hi) / 2;
        if (a[mid] == 3) { lo = hi + 1; }
        else if (a[mid] < 3) { lo = mid + 1; }
        else { hi = mid - 1; }
    }
    System.out.print(mid);
    • 3
    • 1
    • 4
    • 2
    Show worked solution

    Correct answer: 2

    The search examines index 3, then 1, then 2, where it finds the value.

  12. 12.What is printed by the following code segment?

    int[] a = {3, 1, 4};
    int best = -1;
    for (int i = 0; i < a.length; i++)
    {
        if (best == -1 || a[i] > a[best]) { best = i; }
    }
    System.out.print(best);
    • 2
    • 0
    • 1
    • -1
    Show worked solution

    Correct answer: 2

    Short-circuit evaluation seeds the index on the first pass without a separate initialisation step.

  13. 13.What is printed by the following code segment?

    int[] a = {10, 20, 30, 40};
    int c = 0;
    int lo = 0;
    int hi = a.length - 1;
    while (lo < hi)
    {
        c++;
        lo++;
        hi--;
    }
    System.out.print(c);
    • 4
    • 2
    • 3
    • 1
    Show worked solution

    Correct answer: 2

    Two indexes converge from the ends and meet after two steps in a four-element array.

  14. 14.What is printed by the following code segment?

    int[] a = {1, 2, 3};
    int target = 4;
    boolean found = false;
    for (int v : a)
    {
        found = found || v == target;
    }
    System.out.print(found);
    • true
    • The code does not compile.
    • false
    • 4
    Show worked solution

    Correct answer: false

    The accumulating disjunction stays false because no element matches.

  15. 15.What is printed by the following code segment?

    int[] a = {5, 3, 5, 1, 5};
    int c = 0;
    int i = 0;
    while (i < a.length)
    {
        if (a[i] == 5) { c++; i += 2; }
        else { i++; }
    }
    System.out.print(c);
    • 2
    • 5
    • 1
    • 3
    Show worked solution

    Correct answer: 3

    Matching advances the index by two, so index 1 is skipped, but the matches at indexes 0, 2, and 4 are all reached.