File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sun sample mock q

 
sarim raza
Ranch Hand
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic