Correct Answer: E
The code compiles fine.
java.lang.NullPointerException
because only one element of list is initialized : element [0]
elements [1] and [2] equals null
alte Begruendung:
An exception is thrown at runtime due to data type comparison mismatch:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Integer
at java.lang.Integer.compareTo(Integer.java:52)
at java.util.Arrays.binarySearch0(Arrays.java:1481)
at java.util.Arrays.binarySearch(Arrays.java:1423)
at searchtext.SearchText.main(SearchText.java:22)
Note:binarySearch
public static int binarySearch(char[] a,
char key) Searches the specified array of chars for the specified value using the binary search
algorithm. The array must be sorted (as by the sort method, above) prior to making this call. If it is
not sorted, the results are undefined. If the array contains multiple elements with the specified
value, there is no guarantee which one will be found.
Parameters:
a - the array to be searched.
key - the value to be searched for.
Returns:
Index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion
point is defined as the point at which the key would be inserted into the list: the index of the first
element greater than the key, or list.size(), if all elements in the list are less than the specified key.
Note that this guarantees that the return value will be >= 0 if and only if the key is found.