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

public class Tester {

public static void main(String[] args) {

Integer[] arr = { 1, 2, 3 };

System.out.print(Arrays.binarySearch(arr, 1)); // line 1

System.out.print(Arrays.binarySearch(arr, "javachamp")); // line 2

}

}

<< First < Previous Next > Last >>
Explanation:

Method binarySearch has different signatures, the one used in this program is

binarySearch(Object[] a, Object key)

which means it accepts any object type for the second parameter no matter what the type of the array being passed. Therefore line 2; searching for a String in an Integer array; causes no compilation error, but causes ClassCastException at run time when trying to cast the String to an Integer.

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

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
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>