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

Question 19

Given code of Test.java file: 

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

  • A 1.2.
  • B 1.1.1.1.1.
  • C 1.1.
  • D 1.2.3.4.5.
  • E 1.
  • F Nothing is printed on to the console.
Reveal correct answer

Correct answer: C

Explanation

findFirst() is terminal operation.


list.stream() => [-80, 100, -40, 25, 200].


filter(predicate) is executed for each element until just one element passes the test. Because findFirst() will terminate the operation on finding first matching element.


NOTE: a new instance of Predicate is used, hence every time ctr will be initialize to 1.


For -80, Output is '1.' but predicate returns false, hence findFirst() doesn't terminate the operation.


For 100, '1.' is appended to previous output, so on console you will see '1.1.' and predicate returns true, hence findFirst() finds an element and terminates the operation.


Final output is: '1.1.'

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