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
Hi,there:
Let me try to help.
-----------------------------------------------------
i = ((int)Math.ceil(f)) * ((int)Math.round(d));
step1:
Math.ceil(f)=Math.ceil(2.3) will return a double value which is 3.0, we need the expilicty casting to int, so we get 3;
step2:
Math.round(d)=Math.round(2.7) will return a long value which is 3L, we also need to cast it into int, so we get 3,
Step3:
do 3*3; you will get 9.
Hope this would help
[This message has been edited by Helen Yu (edited August 04, 2000).]