• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error in "final" test on cd for RHE book

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found an error with question #27 on a test in the RHE book's cd. The name of the test is "final".
The problem is below:
Math.round(Math.random() + 2.50001)
Answers
A. 2
B. 3
C. It is impossible to say.

RHE says that the answer is B. I chose C. I wrote a simple program below to test this out and had the output that it could be either 3 or 4. This confirmed my suspicion. What do yo think?
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be B. 3. Math.random() always returns a value x where 0 <= x < 1, so the argument of Math.round() is always strictly greater than 2.5 which means it always returns 3.
I found your code difficult to decipher so I wrote my own. I got ten 3's outputted to the screen several times.

[ June 21, 2002: Message edited by: Anthony Villanueva ]
 
Charles Earwood
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply add the following:

2.50001 + .9999999 = 3.5000099
The reason you got 3's is because most of the time the return value of .random() is less than .99999 . In my code, I filter out all values that are less than the value necessary to get a 4.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to Anthony,
However imagine random returns 0.99999 or anything bigger. The result is 4.
[ June 21, 2002: Message edited by: Jose Botella ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoooops! (so much for buying those lottery tickets....)
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
moving this to Mock Exam Errata now that it's been confirmed as a goof...
 
reply
    Bookmark Topic Watch Topic
  • New Topic