Java OCA OCP Practice Question 3007

Question

Choose the correct option based on the following code segment:

Comparator<String> comparer =
        (country1, country2) ->
                country2.compareTo(country2); // COMPARE_TO

String[] brics = {"Brazil", "Russia", "India", "China"};
Arrays.sort(brics, null);
Arrays.stream(brics).forEach(country -> System.out.print(country + " "));
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


c)

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.

the statement marked with COMPARE_TO will compile without errors.

Note that the variable comparer is unused in this code segment.




PreviousNext

Related