Saturday, August 28, 2010

Results of searching an unsorted array are undefined, and sorted arrays have zero-based indexes

Courtesy of Sun Web Learning Center


Given:

 4. import java.util.*;
 5. class Looking {
 6.   public static void main(String[] args) {
 7.     String[] sa = {"d", "c", "a", "b" };
 8.     int x = Arrays.binarySearch(sa, "b");
 9.     Arrays.sort(sa);
10.     int y = Arrays.binarySearch(sa, "b");
11.     System.out.println(x + " " + y);
12.   }
13. }

Which two results are possible? (Choose two.)


A 7 0
B 7 1
C 7 3
D -1 0
E -1 1
F -1 3

B and E are correct. The results of searching an unsorted array are undefined, and sorted arrays have zero-based indexes. Note: Answer e is almost certainly the most common output that would be produced.

No comments:

Post a Comment