Java OCA OCP Practice Question 948

Question

What does the following output?

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


D.

Note

Java requires having a sorted array before calling binarySearch.

Since the array is not sorted, the result is undefined, and Option D is correct.

It may happen that you get 1 as the result, but this behavior is not guaranteed.

You need to know for the exam that this is undefined even if you happen to get the "right" answer.




PreviousNext

Related