Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Hard

Question 39

Given code of Test.java file:

What will be the result of compiling and executing Test class?

  • A

    #####
    #
    ##
    ####
    ###

  • B

    #####
    ####
    ###
    ##
    #

  • C

    #
    ##
    ###
    ####
    #####

  • D

    ###
    ####
    ##
    #
    #####

Reveal correct answer

Correct answer: B

Explanation

Comparator.comparing(s -> s); compares the passed Strings only. As all the characters in the String are '#', this means strings are sorted on the basis of their lengths.

Comparator referred by comp sorts on the basis of strings' lengths.

Default reversed() method just reverses the ordering of the Comparator referred by comp, which means sorts the strings in descending order of their lengths.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need