Originally posted by Renuka Kilambi:
Hi everybody,
This is a question from javaprepare sample exam.
What gets written on the screen when the following program is compiled and run. Select the one right answer.
public class test {
public static void main(String args[]) {
int i;
float f = 2.3f;
double d = 2.7;
i = ((int)Math.ceil(f)) * ((int)Math.round(d));
System.out.println(i);
}
}
a. 4
b. 5
c. 6
d. 6.1
e. 9
The answer is e. I compiled the above code and got the answer 9. can anybody explain how it is to be calculated.
Thanks in advance.
Renuka
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
round gives you the next highest number if the decimal point is 5 or greater( a fractional part which is more than 1/2 ), and the next lowest number if the decimal point is less than 5.
While applying round for negative numbers, ignore the sign, apply the rule and apply the sign again!.
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Originally posted by Ken Lai:
Hi,
For Math.round(d) use this to determine the outcome:
(return_type)(d + 0.5)
so when you have d = -2.5 the result will be:
(return_type)(-2.5 + 0.5) = 2
Note: This is an overloaded method, so if d is of type double return_type would be long, if d is of type float then return_type would be int.
Hope this helps.
Raj