Java OCA OCP Practice Question 3262

Question

Consider the following program:

import java.util.Comparator;
import java.util.Arrays;

class CountryComparator implements Comparator<String> {
        public int compare(String country1, String country2) {
                return country2.compareTo(country2); // COMPARE_TO
        }/*from   w  w w  .j  a v  a  2 s .  c  om*/
}

public class Sort {
        public static void main(String[] args) {
                String[] brics = {"Brazil", "Russia", "India", "China"};
                Arrays.sort(brics, null);
                for(String country : brics) {
                        System.out.print(country + " ");
                }
        }
}

Which one of the following options correctly describes the behavior of this program?

a)The program results in a compiler error in the line marked with the comment COMPARE_TO.
b)The program prints the following: Brazil Russia India China.
c)The program prints the following: Brazil China India Russia.
d)The program prints the following: Russia India China Brazil.
e)The program throws the exception InvalidComparatorException.
f)The program throws the exception InvalidCompareException.
g)The program throws the exception NullPointerException.


b)

Note

For the sort() method, null value is passed as the second argument, which indicates that the elements' "natural ordering" should be used.

In this case, natural ordering for Strings results in the strings sorted in ascending order.

Note that passing null to the sort() method does not result in a NullPointerException.




PreviousNext

Related