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

Question 20

Given code of Test.java file: 

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

  • A []
  • B [{S001, OCA, 87}, {S002, OCA, 82}, {S001, OCP, 79}, {S002, OCP, 89}, {S003, OCA, 60}, {S004, OCA, 88}]
  • C [{S001, OCP, 79}, {S002, OCP, 89}]
  • D [{S001, OCA, 87}, {S002, OCA, 82}, {S003, OCA, 60}, {S004, OCA, 88}]
Reveal correct answer

Correct answer: A

Explanation

Rest of the code is very simple, let us concentrate on partitioning code.


Collectors.partitioningBy(s -> s.equals("OCA")) => s in this lambda expression is of Certification type and not String type.

This means predicate 's -> s.equals("OCA")' will return false for "OCA". None of the certification object will return true and hence no element will be stored against 'true'.


[] will be printed in the output.


Correct predicate will be: 's -> s.getTest().equals("OCA")'.


For above predicate, output for 'System.out.println(map.get(true));' will be:

[{S001, OCA, 87}, {S002, OCA, 82}, {S003, OCA, 60}, {S004, OCA, 88}]

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