programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# sun sample mock q

sarim raza
Ranch Hand
Posts: 232
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);

//CORRECT ANSWER IS C AND E, WHY IS E CORRECT ??
WHAT IS THE d mean ?

Ashish Agarwal
Ranch Hand
Posts: 36
floor() returns the largest double value that is not greater than
argument of floor() and is equal to an integer.
d is used to signify double value though it is not needed as floating values are implicitly double it is given just confuse.
Ashish

sonal gupta
Greenhorn
Posts: 4
choice d gives an error b'coz it has a min method() which gives a minimum value of 2 but here is only a one value,it just adding a double value in pi.
sonal

Tom Tang
Ranch Hand
Posts: 133
answer c and e is basically the same. The API's definition for Math.round() is (long)Math.floor(a+0.5d);

sarim raza
Ranch Hand
Posts: 232
thanks for the effort guys, but my question was WHY IS E CORRECT ?
i did not get an answer to that, i know how the floor works, but why is the value of pi, added to 0.5d, what is 0.5d, where does it come from , what does it represent ?
if it is a double value of 0.5, then after performing round, we would get 4 as a result and not 3, explain ?

Tom Tang
Ranch Hand
Posts: 133
You perform floor(not round) after adding 0.5d, that's why it has the same effect as rounding numbers. The round method comes from floor. Hope this helps.

sarim raza
Ranch Hand
Posts: 232
i am still not clear on this, why is the 0.5d value used, and wouldnt the result after rounding in this case be 4, instead of 3 ?

Tom Tang
Ranch Hand
Posts: 133
0.5d value is used because it can achieve the rounding effect with floor. Choice e is :int p = (int)Math.floor(pi + 0.5d); No round method is used here and p will be floored to 3 just like the round method in choice c.

sunilkumar ssuparasmul
Ranch Hand
Posts: 142
D is incorrect bcos Math.min(a,b) takes 2 arguments where a,b can be double , int ,long or float. but here it takes only one arguments..
HTH
sunil.s
------------------
"Winners don't do different things
They do things differently"