Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Math Doubt

 
Archana Nair
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Answer a
This a question in barry bones mock exam(q no 31)
the answer is given as a .But i thgt the answer is c) and not a) because when ceil and round is used then the answer should me 15L and not 15.0.please explain.
 
Asma Zafar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why the answer is 'a' is:
The value is assigned to a double variable, therefore the long value of the round method (15L) will be automatically promoted to double (following the rules of widening conversion).
Hope this helps.
------------------
Asma Zafar,
Sun Certified Programmer for Java2 Platform
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think ans c is correct since round () will return 15 not 15.0
Vanitha.
 
Percy Densmore
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vanitha has a point there.

I remember from RoundUp Quiz that Math.round(double) will return a long, Math.round(float) will return an int.
However, I went and checked on the code in question and notice that the long return will implicitly be cast to double b.

Answer a will work in this case. Soo the winner is Asma
------------------
Percy Densmore
-SCJP2 Die Hard Student
[This message has been edited by Percy Densmore (edited September 08, 2001).]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am sorry I didn't see the line double b = method(a).
The first option will work too.
Vanitha.
 
Archana Nair
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Even i didnt notice the line double b=method(a);
Anyway now my doubt is cleared with Asma's explanation.Thanx guys!
Archana
 
PANDORA
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vanitha Sugumaran:
Hi all,
I am sorry I didn't see the line double b = method(a).
The first option will work too.
Vanitha.

I think so
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic