Java OCA OCP Practice Question 869

Question

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. 0
  • B. 1
  • C. 2
  • D. The output is not defined.


A.

Note

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.




PreviousNext

Related