Author Topic:   Boone mock: Math.
Rolf Weasel
ranch hand
posted March 10, 2000 08:11 PM             
i found the following question in the Boone mock:

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


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
bartender
posted March 10, 2000 09:36 PM             
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
sheriff
posted March 10, 2000 10:00 PM             
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
bartender
posted March 10, 2000 10:25 PM             
yes. I didn't see the the assignment part properly. Thanks Tony.

|