Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Mock exam question

Anonymous
Ranch Hand
Posts: 18944

What will happen if you invoke the following method?
1: public void check()
2: {
3: System.out.println(Math.min(-0.0,+0.0));
4: System.out.println(Math.max(-0.0,+0.0));
5: System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0));
6: }
A) prints -0.0, +0.0 and false.
B) prints -0.0, +0.0 and true.
C) prints 0.0, 0.0 and false.
D) prints 0.0, 0.0 and true.
why the right answer is B not A?
thanks

Manfred Leonhardt
Ranch Hand
Posts: 1492
Hi,
Direct from the JLS:

Positive zero and negative zero compare equal; thus the result of the expression 0.0==-0.0 is true and the result of 0.0>-0.0 is false. But other operations can distinguish positive and negative zero; for example, 1.0/0.0 has the value positive infinity, while the value of 1.0/-0.0 is negative infinity.

Regards,
Manfred.

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
dianyjava,
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name.
Thanks
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums

Dian He
Greenhorn
Posts: 12

Hi Manfred,
Thanks for your explanation, I understood now, thanks again.
Regards
Dian He

Hi Thomas,
Sorry about the name, I have changed it now, I wish I will be one good member of this professional forum.
Regards
Dian He