Recursive Searching and Sorting (Binary Search, Merge Sort)

AP Computer Science A· difficulty 4/5

Which statement about Java's typical binary-search algorithm is TRUE?

  • A

    It fails with a runtime exception if the target is missing from the array.

  • B

    It runs in O(log n) time even on unsorted arrays as long as you use Integer.compare.

  • C

    It requires the input array to be sorted; otherwise the result is undefined.

    check_circle
  • D

    Calling it on a duplicate-free unsorted array still works because the algorithm rechecks order.

Explanation

Binary search has a strict PRECONDITION: the array must be sorted. If the precondition is violated the result is undefined (it does not throw). When the target is missing, standard implementations return an "insertion-point" indicator like -(insert)-1, not an exception.

Want 10 more like this — adaptive to your weak spots?

Related questions