Given:
3. import java.util.*; 4. public class Main { 5. public static void main(String[] args) { 6. String[] s = {"map", "pen", "marble", "key"}; 7. MyClass o = new MyClass(); 8. Arrays.sort(s,o);// ww w . j a v a 2 s .co m 9. for(String s2: s) System.out.print(s2 + " "); 10. System.out.println(Arrays.binarySearch(s, "map")); 11. } 12. static class MyClass implements Comparator<String> { 13. public int compare(String a, String b) { return b.compareTo(a); } 14. } 15. }
Which are true?
Choose all that apply.
D and G are correct.
First, the compareTo()
method will reverse the normal sort.
Second, the sort()
is valid.
Third, the binarySearch()
gives -1 because it needs to be invoked using the same Comparator (o) as was used to sort the array.
Note that when the binarySearch()
returns an "undefined result," it doesn't officially have to be a -1, but it usually is, so if you selected only G, you get full credit!