Test: Computer Science

public static int foo(int[] arr, int x) {

    int a = 0;

    int b = arr.length - 1;

    while(b >= a) {

        int c = (a + b) / 2;

        int v = arr[c];

        

        if(v == x) {

            return c;

        } else if(v < x) {

            a = c + 1;

        } else {

            b = c - 1;

        }

    }

    return -1;

}

1.

What is the value of y in the code below:

int[] vals = {1,3,4,5,6,31,41,51};

int x = 41;

int y = foo(vals,41);

41

5

6

-1

7

1/3 questions

0%
Learning Tools by Varsity Tutors