Java OCA OCP Practice Question 3104

Question

Consider the following program:

import java.util.Arrays;

public class Main {
        public static void main(String[] args) {
               String[] brics = {"Brazil", "Russia", "India", "China"};
               Arrays.sort(brics, null);       // LINE A
               for(String country : brics) {
                       System.out.print(country + " ");
               }/*from   ww w. j a va 2s. co  m*/
        }
}

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

a) This program will result in a compiler error in line marked with comment LINE A.
b) When executed, the program prints the following: Brazil Russia India China.
c) When executed, the program prints the following: Brazil China India Russia.
d) When executed, the program prints the following: Russia India China Brazil.
e) When executed, the program throws a runtime exception of NullPointerException when executing the line marked with comment LINE A.
f) When executed, the program throws a runtime exception of InvalidComparatorException when executing the line marked with comment LINE A.


c)

Note

When null is passed as a second argument to the Arrays.sort() method, it means that the default Comparable (i.e., natural ordering for the elements) should be used.

The default Comparator results in sorting the elements in ascending order.

The program does not result in a NullPointerException or any other exceptions or a compiler error.




PreviousNext

Related