OCA Java SE 8 Core Java APIs - OCA Mock Question Core Java APIs 3-8








Question

What is the result of the following?

public class Main{
   public static void main(String[] argv){
     int[] random = { 16, -41, 112, 10, -110 }; 
     int x = 112; 
     int y = Arrays.binarySearch(random, x); 
     System.out.println(y); 
   }
}
  1. 2
  2. 4
  3. 6
  4. The result is undefined.
  5. An exception is thrown.
  6. The code does not compile.




Answer



D.

Note

The code compiles and runs fine.

We can only binary search an sorted array for a meaningful result.