7. Given double pi = Math.PI; Which two are valid ways to round pi to an int?(Choose two.) A.int p = pi; B.int p = Math.round(pi); C.int p = (int)Math.round(pi); D.int p = (int)Math.min(pi + 0.5d); E.int p = (int)Math.floor(pi + 0.5d);
This topic has been discussed last days , i just cannot find thread. Gonna be B and E???
I agree the second part is the more interesting but for the first part b does not work. There are two methods for round but as I read the question it is not calling the method that returns an int. It is calling round with an argument that is a double. This returns a long which requires a cast to be squeezed into an int.
Scott, you right. round(double) - Static method in class java.lang.Math Returns the closest long to the argument. round(float) - Static method in class java.lang.Math Returns the closest int to the argument. Link: http://suned.sun.com/USA/certification/progsqwa.html
[This message has been edited by Vladimir Kositsky (edited January 07, 2001).]
I think the answer is C & E. C, because, without casting result would be 3.00 and with (int) cast result would be 3 E, because, without cast, result would be floor(3.14 + 0.5= 3.64), which equals 3.00 and with cast(int) result would be 3 Agree??? Thanks. Nielsh