programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Sun Sample question #7

Ranch Hand
Posts: 59
• Number of slices to send:
Optional 'thank-you' note:
Hi,
SAMPLE QUESTION 7:
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);
Mukti

[This message has been edited by Mukti Bajaj (edited January 22, 2001).]

"The Hood"
Posts: 8521
• Number of slices to send:
Optional 'thank-you' note:
pi is a double.
A. You can't shove a double into an int without casting.
B. Same as A. Rounding a double does not make it anything other than a double.
C. This does the cast from double to int - Good
D. The method min() takes 2 values separated by a comma and gives you back the lesser of the two. This example only has one input value (the result of the addition).
E. If you read the definition of what round does, it is exactly this arithmatic, as documented below.
The API says:

public static long round(double a)
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)
Special cases:
If the argument is NaN, the result is 0.
If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.
If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.

[This message has been edited by Cindy Glass (edited January 23, 2001).]

 A wop bop a lu bob a womp bam boom. Tutti frutti ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising