Unit 4 · Data Collections
Linear and Binary Search
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
What linear and binary search covers
Linear search checks elements one at a time and works on any data, taking up to n comparisons. Binary search requires sorted data and repeatedly halves the range: compute the midpoint, compare, then discard the half that cannot contain the target. The bounds must exclude the midpoint on each update, using low = mid + 1 or high = mid - 1, and the loop continues while low is less than or equal to high.
Where students lose points
Applying binary search to unsorted data is the conceptual error the exam tests most. Mechanically, setting low = mid or high = mid keeps the midpoint in range and can loop forever. Students also miscount comparisons by forgetting that the midpoint check itself counts as one.
How it shows up on the AP exam
Unit 4 content. Questions that ask which indexes binary search examines for a given target, or how many comparisons each algorithm needs, are a standard and very predictable MCQ format.
For practice use only.
Linear and Binary Search MCQ Practice
AP CSA linear and binary search practice: comparison counts, the sorted-data requirement, midpoint updates, and correct loop termination.
Answered 0 of 15
Choose one answer.
Which option matches the complete output produced by this segment?
public class LinearAndBinarySearchContext
{
public static void runExample()
{
int[] a = {2, 5, 9, 14};
int lo = 0;
int hi = a.length - 1;
int mid = (lo + hi) / 2;
System.out.print(mid + " " + a[mid]);
}
public static void main(String[] args)
{
runExample();
}
}