aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes k&b doubt 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "k&b doubt 2" Watch "k&b doubt 2" New topic
Author

k&b doubt 2

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hi in chapter 6...

in one question...


Given the following,
1. public class Example {
2. public static void main(String [] args) {
3. double values[] = {-2.3, -1.0, 0.25, 4};
4. int cnt = 0;
5. for (int x=0; x < values.length; x++) {
6. if (Math.round(values[x] + .5) == Math.ceil(values[x])) {
7. ++cnt;
8. }
9. }
10. System.out.println("same results " + cnt + " time(s)");
11. }
12. }
what is the result?
A. same results 0 time(s)
B. same results 2 time(s)
C. same results 4 time(s)
D. Compilation fails.
E. An exception is thrown at runtime.


its anser is "B"

but don't understand why... i am not able to understand its explaination...
kindly clear the doubt...

thanx


Thanks and Regards, Amit Taneja
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Try to run this code.

[ May 10, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
alzamabar
Ranch Hand

Joined: Jul 24, 2002
Posts: 379
Originally posted by amit taneja:
hi in chapter 6...

in one question...


Given the following,
1. public class Example {
2. public static void main(String [] args) {
3. double values[] = {-2.3, -1.0, 0.25, 4};
4. int cnt = 0;
5. for (int x=0; x < values.length; x++) {
6. if (Math.round(values[x] + .5) == Math.ceil(values[x])) {
7. ++cnt;
8. }
9. }
10. System.out.println("same results " + cnt + " time(s)");
11. }
12. }
what is the result?
A. same results 0 time(s)
B. same results 2 time(s)
C. same results 4 time(s)
D. Compilation fails.
E. An exception is thrown at runtime.


its anser is "B"

but don't understand why... i am not able to understand its explaination...
kindly clear the doubt...

thanx


The logic is as follows:

-2.3 and 0.25 are the correct answers.

For -2.3:

(-2.3 + .5) == -1.7
-1.7 Rounded == -2.0

-2.3 Ceiled == -2

Therefore -2.0 == 2


For 0.25:

(0.25 + .5) == 0.75
0.75 Rounded == 1.0

0.25 Ceiled == 1

Therefore 1.0 == 1


-1.0 is not correct because:

(-1.0 + .5) == -.5
-.5 Rounded == 0

-1.0 Ceiled == -1.0

Therefore 0 != -1.0


4 Is incorrect because:

(4 + .5) == 4.5
4.5 Rounded == 5

4 Ceiled == 4

Therefore 4 != 5


Marco Tedone<br />SCJP1.4,SCJP5,SCBCD,SCWCD
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Therefore -2.0 == 2

It should be -2.0 == -2.0
alzamabar
Ranch Hand

Joined: Jul 24, 2002
Posts: 379
Originally posted by Srinivasa Raghavan:

It should be -2.0 == -2.0


No, it should be -2.0 == 2

From Java Language Specification (chapter 5.6.2):


If either operand is of type double, the other is converted to double.



Therefore 2 is promoted to be 2.0 and the two results are equal.

You can run the following code for testing:

Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Yep i agree this

If either operand is of type double, the other is converted to double.

But it should be negative right or am i going wrong any where ?
I mean -2.0 == -2
[ May 10, 2005: Message edited by: Srinivasa Raghavan ]
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
thanx
[ May 10, 2005: Message edited by: amit taneja ]
alzamabar
Ranch Hand

Joined: Jul 24, 2002
Posts: 379
Originally posted by Srinivasa Raghavan:
Yep i agree this

If either operand is of type double, the other is converted to double.

But it should be negative right or am i going wrong any where ?
I mean -2.0 == -2

[ May 10, 2005: Message edited by: Srinivasa Raghavan ]


Yes it should be negative. Sorry for the typo
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: k&b doubt 2