Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Boone mock: Math.  RSS feed

 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found the following question in the Boone mock:

Question 32: Given this code snippet:<CODE>
double a = 14.9;
double b = method(a);
System.out.println(b);</CODE>

If this snippet displays 15.0 in the standard output, what Math method(s) could method() have invoke?
Select the one right answer.
a. ceil() and round()
b. floor() and round()
c. ceil() only
d. floor() only
e. round() only
Question 32: c. Only ceil(). Round() will produce 15 from 14.9 not 15.0. The floor() method yields 14. (See chapter 10.)

In the code, the result of method() is cast to the double b before printing. Hence, the result of round(a) will be promoted to 15.0. The answer should be (a).

 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Only answer c) is correct. Math.round(double d) method returns a long, not double. Math.round(float f) returns an int. Test and see.
regds
maha anna
[This message has been edited by maha anna (edited March 10, 2000).]
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Math.round(a) returns a long which is converted to double automatically because of implicit widening. So what will be DISPLAYED is 15.0 in both cases (Math.round and ceil).
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. I didn't see the the assignment part properly. Thanks Tony.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!