This week's book giveaway is in the Android forum.
We're giving away four copies of Create an Uber Clone in 7 Days: Build a real world full stack mobile app in Java and have Shai Almog on-line!
See this thread for details.
Win a copy of Create an Uber Clone in 7 Days: Build a real world full stack mobile app in Java this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BB Question 32  RSS feed

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 32:
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

Author chooses a.
I think the answer should be c because the snippet displays a floating point number 15.0 rather than int or long 15. If round() is added as indicated by a , the display would be 15, not 15.0 anymore.
Am i right? Seek your confirmation or correction. Thanks a lot.

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I agree with you. Answer should be C only.
Jyotsna
 
Daniel Wu
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, refer to the previous topic, and it's helpful.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct answer is 'a'.
<code>Math.round(double)</code> returns a <code>long</code> however <code>b</code> has a declared type of <code>double</code> . The <code>long</code> value will be promoted to <code>double</code> and displayed as <code>15.0</code> not <code>15</code>.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited June 17, 2001).]
 
Daniel Wu
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jane Griscti:
The correct answer is 'a'.
<code>Math.round(double)</code> returns a <code>long</code> however <code>b</code> has a declared type of <code>double</code> . The <code>long</code> value will be promoted to <code>double</code> and displayed as <code>15.0</code> not <code>15</code>.
Hope that helps.


Jane, your explanation for option a is right.
How about option c "ceil()only"? I think it is right too. it produce 15.0 which is a double already. Thanks.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,
Your kidding right! If we read the question you typed in your first post we see the instructions:
Select the one right answer
If a is the correct answer (as you stated to Jane), do you still think c is also an answer?
Always follow the instructions ... they are there to help you.
Regards,
Manfred.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I am seeing here is that "a" is the correct answer, as round() and ceil(), produce the same result i.e 15.0, if used individually.
--Farooq
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
variable is of Double type so it convert 15 to 15.0 automaticaly
Amit
 
brevity is the soul of wit - shakepeare. Tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!