info@meritcampus.com    +91-85006-22255
...
`

Recursion-1906
What will be the output of the following program?
`public class ReRecursion {    public static int recursiveBinarySearch(int[] ary, int point1, int point2, int key) {        if (point1 < point2) {            int point = point2 + (point2 - point1) / 2;            if (key < ary[point]) {                return recursiveBinarySearch(ary, point1, point, key);            } else if (key > ary[point]) {                return recursiveBinarySearch(ary, point + 1, point2, key);            } else {                return point;            }        }        return -(point1 + 1);    }    public static void main(String[] args) {        int[] arr1 = {2, 45, 234, 567, 876, 900, 976, 999};        int index = recursiveBinarySearch(arr1, 0, arr1.length, 45);        System.out.println("Found 45 at " + index + " index");        index = recursiveBinarySearch(arr1, 0, arr1.length, 999);        System.out.println("Found 999 at " + index + " index");    }}`

 A. Found 45 at 2 indexFound 999 at 8 index B. Found 45 at 1 indexFound 999 at 7 index C. Some other output D. Compilation Error or Runtime Error
Topic:

### User comments below.All of them might not be correct.

Ans is D. ArrayIndexOutOfBoundsException arises. for the first recursive call, point1 (0) is less than point2 (8 = length of the int array) so point is calculated as 8 + (8-0)/2 which gives 12 and in the next step we are referring to ary[12] which does not exist and throws exception

Posted by Nikhil Sulibhavi    2014-05-05 12:56:41

arry index out of bound exception
bcz in point = point 2+(point 2-point 1)/2;
here point value get high value then arry length in the 1st attempt
ans : d

Posted by Ananda Kumar    2014-05-05 13:02:02

ans is D , because of array index outof bound when key <ary[point] and in the main function at index..., it might be show an error as array index out of bound xception.

Posted by Saikiran Vavilala    2014-05-05 13:03:43

Congratulations Nikhil Sulibhavi. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-06 04:22:07