Java OCA OCP Practice Question 567

Question

What does this code output?

String[] nums = new String[] { "1", "9", "10" }; 
Arrays.sort(nums); 
System.out.println(Arrays.toString(nums)); 
  • A. [1, 9, 10]
  • B. [1, 10, 9]
  • C. [10, 1, 9]
  • D. None of the above


B.

Note

The elements of the array are of type String rather than int.

Therefore, we use alphabetical order when sorting.

The character 1 sorts before the character 9, alphabetically making Option A incorrect.

Shorter strings sort before longer strings when all the other characters are the same, making Option B the answer.




PreviousNext

Related