Java OCA OCP Practice Question 597

Question

What does the following output?

String[] os = new String[] { "Mac", "Linux", "Windows" }; 
Arrays.sort(os); 
System.out.println(Arrays.binarySearch(os, "Mac")); 
  • A. 0
  • B. 1
  • C. 2
  • D. The output is not defined.


B.

Note

The code sorts before calling binarySearch(), so it meets the precondition for that method.

The target string of "Mac" is the second element in the sorted array.

Since array indices begin with zero, the second position is index 1, and Option B is correct.




PreviousNext

Related