About Question enthuware.ocpjp.v8.2.1810 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
sorabhm
Posts: 2
Joined: Wed May 24, 2017 8:20 pm
Contact:

About Question enthuware.ocpjp.v8.2.1810 :

Post by sorabhm »

List<Person> friends = Arrays.asList(new Person("Bob", 31), new Person("Paul", 32), new Person("John", 33));
double averageAge = friends.stream().filter(f->f.getAge()<30)
.mapToInt(f->f.getAge()).average().getAsDouble();
System.out.println(averageAge);

As per the logic, "average()" method returns OptionalDouble and if it is empty then getAsDouble throws an exception. Though the explanation correctly explains this but the answer selected in the test is incorrect. It still shows 0.0 as the correct answer.
Any comments?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1810 :

Post by admin »

I see that option 2 (It will throw an exception at runtime. ) is set as the correct option and not option 3 (0.0). Can you please post a screen shot if you see anything different?
-Paul.
If you like our products and services, please help us by posting your review here.

sorabhm
Posts: 2
Joined: Wed May 24, 2017 8:20 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1810 :

Post by sorabhm »

Apologies for the delay. I got the reason for confusion here. Screenshot attached below. As per comments it is mentioned that

"Since there is no element in the stream anyway, the stream remains empty. Finally, the average() method computes the average of all the elements, which is 0.0."

I read the documentation and it is fine that in case of an empty stream OptionalDouble.empty will be returned. Below is the JavaDoc snippet.

/**
* Returns an {@code OptionalDouble} describing the arithmetic mean of elements of
* this stream, or an empty optional if this stream is empty. This is a
* special case of a
* <a href="package-summary.html#Reduction">reduction</a>


The above text will need some correction to clearly mention the way it is described in JavaDoc.

Thanks
Sorabh
Attachments
Screenshot
Screenshot
question_java8.JPG (131.33 KiB) Viewed 4452 times

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests