What does the following output?
import java.util.Arrays; public class Main { public static void main(String[] args) { String[] os = new String[] { "Linux", "Mac", "Windows" }; System.out.println(Arrays.binarySearch(os, "Linux")); } }
A.
Java requires having a sorted array before calling binarySearch()
.
You do not have to call Arrays.sort to perform the sort though.
This array happens to already be sorted, so it meets the precondition.
The target string of "Linux" is the first element in the array.
Since Java uses zero-based indexing, the answer is Option A.