What is the result?

Given:

What is the result?

Given:

What is the result?

A.
1

B.
0

C.
2

D.
Compilation fails

E.
An exception is thrown at runtime

Explanation:
The code compiles fine.
java.lang.NullPointerException
because only one element of list is initialized : element [0]
elements [1] and [2] equals null
alte Begründung:
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 mustbe sorted (as by the sort method, above) prior to making this call. If it is
not sorted, the results are undefined. Ifthe 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:
Indexof the search key, if it is contained in the list; otherwise, (-(insertion point) – 1). The
insertionpoint is defined as the point at which the key would be inserted into the list: the index of
the first elementgreater than the key, or list.size(), if all elements in the list are less than the
specified key. Note that thisguarantees that the return value will be >= 0 if and only if the key is
found.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Tim

Tim

E, with NullPointerException.

peter

peter

If
array[0]= “foo”;
array[1]= 1;
array[2]= ‘a’;

E is still the answer with the following exception
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer