Searching Flashcards

1
Q

Give the signature of the Arrays.binarySearch method !

A

The Arrays.binarySearch method in Java is used to search for a specific element in a sorted array.

Here are the key method signatures for Arrays.binarySearch:

public static int binarySearch(int[] a, int key)

public static int binarySearch(double[] a, double key)

public static int binarySearch(float[] a, float key)

public static int binarySearch(String[] a, String key)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give a characteristic of the Arrays.binarySearch method !

A

Arrays.binarySearch gives the right result only if the array that gets passed to it is a sorted array

If the element is found, the method returns the index of the element in the array.

If the element is not found, the method returns a negative value. Specifically, it returns -(insertion point) - 1, where the insertion point is the index at which the element would be inserted to maintain sorted order.

The exam creators will not expect you to know what incorrect values come
out. As soon as you see the array isn’t sorted, look for an answer choice about unpredictable output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly