Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# error in BB mock exam!

ego hu
Ranch Hand
Posts: 53
In Barry Boone's mock exam:
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?
a.ceil() and round()
b.floor() and round()
c.ceil() only
d.floor() only
e.round() only

Question 32: a. Only ceil() & Round() will produce 15.0 from 14.9. The floor() method yields 14. (See chapter 10.)
In fact, Math.round() returns long type, so it will produce
15, but not 15.0.
I wrote a programme to check it. I think I am right.

bill bozeman
Ranch Hand
Posts: 1070
You are right that round will produce an int or long, but since he says:
double b = ....
The result will get upcasted to a double, so 15.0 should print out.
Bill

ego hu
Ranch Hand
Posts: 53
Sorry.
Got it.

bill bozeman
Ranch Hand
Posts: 1070
No problem. It is good that you remembered that round returns a long. I could never seem to remember what round, ceil, floor, and random returned.
Bill

Daniel Wu
Ranch Hand
Posts: 58
So Both a and c are right for both produce a printout of 15.0. Am i right?

Jane Griscti
Ranch Hand
Posts: 3141
Daniel,
Both Math.round() and Math.ceil() will result in an output of 15.0 so only answer 'a' is correct. Answer 'c' say only ceil() will result in 15.0
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Javaï¿½ 2 Platform

Daniel Wu
Ranch Hand
Posts: 58
Got you, Jane, thanks.