Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/using-binarysearch-with-an-array-in-java
Question 196 / 297:  What is the result of compiling and running the following code?
import java.util.Arrays;

public class Tester{

public static void main(String[] args) {

String[] arr = { "java", "champ", "champion" };

Arrays.sort(arr);

System.out.print(Arrays.binarySearch(arr, "champion"));

System.out.print(Arrays.binarySearch(arr, "You"));

}

}

A  10
B  1-1
C  un expected result
D  20
E  2-1
F  compilation error
<< First < Previous Next > Last >>
Explanation:

Arrays.sort(arr) will sort the array as following:

{ "champ", "champion", "java" }

therefore the first binary search will return 1

The second search will return the appropriate index for word "You", if it would be inserted in this array and keeping it sorted.

Since upperCase letters are smaller than lowercase letters, then the appropriate insertion for word "You" in the array would be before the word "java" at index 0.The appropriate index for word "You" is calculated as (-insertion index - 1)

which gives us -1 as a result

Exam Home Page
https://www.jobilize.com/java-certification-questions

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Vanessa Soledad
Start Exam
David Bourgeois
Start Quiz
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>