Java OCA OCP Practice Question 2695

Question

What is the result of the following program?

import java.util.*; 

public class Main implements Comparator<String> { 
   public int compare(String a, String b) { 
      return b.toLowerCase().compareTo(a.toLowerCase()); 
   } //from   w w w.  j  a  va2  s.c o  m

   public static void main(String[] args) { 
      String[] values = { "123", "Abb", "aab" }; 
      Arrays.sort(values, new Main()); 
      for (String s: values) 
         System.out.print(s + " "); 
   } 
} 
A.  Abb aab 123
B.  aab Abb 123
C.  123 Abb aab
D.  123 aab Abb
E.  The code does not compile.
F.  A runtime exception is thrown.


A.

Note

The array is sorted using Main, which sorts the elements in reverse alphabetical order in a case-insensitive fashion.

Normally, numbers sort before letters.

This code reverses that by calling the compareTo() method on b instead of a.




PreviousNext

Related