• Post Reply Bookmark Topic Watch Topic
  • New Topic

Math Doubt  RSS feed

 
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.
 
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
 
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.
 
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
 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!