Skip to main content

Unit 4 · Data Collections

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.

Which option matches the complete output produced by this segment?

public class ArraySearchContext
{
    public static void runExample()
    {
        int[] a = {4, 7, 2};
        int found = -1;
        for (int i = 0; i < a.length; i++)
        {
            if (a[i] == 7)
            {
                found = i;
            }
        }
        System.out.print(found);
    }

    public static void main(String[] args)
    {
        runExample();
    }
}
Choose one answer.