Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Math Class

 
Richard Rex
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all ranchers,

While having a practice on one of the mock exams i got free over the net, I found this question confusing:

What will be the output of the code assuming embedded on a complete program.

Float f1 = new Float("4.4e99f");
Float f2 = new Float("-4.4e99f");
Double d1 = new Double("4.4e99");
System.out.println(f1);
System.out.println(f2);
System.out.println(d1);

a) Runtime error
b) Infinity
-Infinity
4.4E99
c) Infinity
-Infinity
Infinity
d) 4.4E99
-4.4E99
4.4E99

I answered D, but the correct answer says it's B. I also tried running this is my editor and it's really B. I looked for the answer explanation in the mock exam but unfortunately I got no explanation why.

Can somebody explain why the answer is B?
 
Gokul Somasundaram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope the answer is simple. The float range is 3.4e-038 to 3.4 e+038. So e+99 is greeater than e+38. So the float value became infinite.
 
Richard Rex
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gokul Somasundaram,

Thanks a lot! Sorry I forget to consider the range. Now it's clear to me.
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranges of float and double are xcluded from exam.I mean You won't find such questions.So Don't expect such brilliant question.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1603
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks agrah! That is what even i was wondering about.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic