Kathy Wang

Greenhorn

Posts: 28

posted 15 years ago

Hello,

In the final exam Q27:

What is the value of the following expression?

Math.round(Math.random()+2.50001);

A. 2;

B. 3;

C. It is impossible to say.

Ans is B.

To my opinion, the answer is C. Because Math.random() returns double that greater than or equal to 0.0 and less than 1.0, and Math.round() return (long)Math.floor(a + 0.5d). The a is stuff in the Math.round(a). Thus if the returned double from Math.random() is 0.99999, then a in the Math.round is 3.5, at last the Math.round() returns 4, not 3. Where is my problem on understanding this question? Can anyone please help me out? Thanks.

In the final exam Q27:

What is the value of the following expression?

Math.round(Math.random()+2.50001);

A. 2;

B. 3;

C. It is impossible to say.

Ans is B.

To my opinion, the answer is C. Because Math.random() returns double that greater than or equal to 0.0 and less than 1.0, and Math.round() return (long)Math.floor(a + 0.5d). The a is stuff in the Math.round(a). Thus if the returned double from Math.random() is 0.99999, then a in the Math.round is 3.5, at last the Math.round() returns 4, not 3. Where is my problem on understanding this question? Can anyone please help me out? Thanks.

ashok khetan

Ranch Hand

Posts: 153

posted 15 years ago

hi!

i too was just going to post the same qu.....i also thought the possibility of returning 0.99999 by Math.random()...so the right answer should be C.

ashok.

i too was just going to post the same qu.....i also thought the possibility of returning 0.99999 by Math.random()...so the right answer should be C.

ashok.

Valentin Crettaz

Gold Digger

Sheriff

Sheriff

Posts: 7610

posted 15 years ago

Yep me too I'm not convinced that B is the right answer...

I ran some tests and the values produced range from 2.50001 to some double primitive slightly bigger than 3.5

So I too think that C is the right answer

Val

I ran some tests and the values produced range from 2.50001 to some double primitive slightly bigger than 3.5

So I too think that C is the right answer

Val

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML

[Blog] [Blogroll] [My Reviews] My Linked In

Amit Agrawal

Ranch Hand

Posts: 282

posted 15 years ago

I don't know as i don't have any logical solution to it but I ran this line in a loop for 10000 times and printed its value and every time the output was 3 only (but than its random number generation so i might have never got 0.99999).

personally i think the correct answer should have been c.

personally i think the correct answer should have been c.

Kathy Wang

Greenhorn

Posts: 28

Jane Griscti

Ranch Hand

Posts: 3141

posted 15 years ago

Hi guys,

See this earlier post on same topic.

The post also contains a link to the errata.

------------------

Jane Griscti

Sun Certified Programmer for the Javaï¿½ 2 Platform

See this earlier post on same topic.

The post also contains a link to the errata.

------------------

Jane Griscti

Sun Certified Programmer for the Javaï¿½ 2 Platform

Jane Griscti

SCJP, Co-author Mike Meyers' Java 2 Certification Passport

posted 15 years ago

That's a pretty lazy answer on the Sybex site, too.

For the record, I proposed that we re-word the question altogether. It's of course entirely possible to say what the resulting value could be. The problem is with the set of answers themselves; a correct answer is missing from the choice list.

------------------

Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

For the record, I proposed that we re-word the question altogether. It's of course entirely possible to say what the resulting value could be. The problem is with the set of answers themselves; a correct answer is missing from the choice list.

------------------

Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

*Make visible what, without you, might perhaps never have been seen.*

- Robert Bresson

It is sorta covered in the JavaRanch Style Guide. |