File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

See this question

 
Ja vardhan
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the one from K&B 1.4 book:

Which three are valid declarations of a float? (Choose three.)
A. float f1 = -343;
B. float f2 = 3.14;
C. float f3 = 0x12345;
D. float f4 = 42e7;
E. float f5 = 2001.0D;
F. float f6 = 2.81F;

The answer is A,D,F .

Can you pleas explain why the options A,D are correct and B,C are not correct ?

Thanks.
 
Tamara Lopez
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ja,

A. float f1 = -343;
B. float f2 = 3.14;
C. float f3 = 0x12345;
D. float f4 = 42e7;
E. float f5 = 2001.0D;
F. float f6 = 2.81F;

The answer is A,D,F .

AD BC

B is not correct because floating-point numbers are considered doubles. If you want a float you must mark it as such. 3.14F
C is not correct because you can only have ints in hex or octal format.
A is correct because an int fits into a float. (it's not a floating point number so you don't need the F is this case)
D.....erm - you can just represent floats with the e scientific notation. again you dont need the f because its not a floating point number.
 
Win Siu
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A. float f1 = -343;
B. float f2 = 3.14;
C. float f3 = 0x12345;
D. float f4 = 42e7;
E. float f5 = 2001.0D;
F. float f6 = 2.81F;

How
 
Win Siu
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A. float f1 = -343;
B. float f2 = 3.14;
C. float f3 = 0x12345;
D. float f4 = 42e7;
E. float f5 = 2001.0D;
F. float f6 = 2.81F;

How come when I tried it with Eclipse, it marked
A, C and F to be correct instead?

I think D is not correct. A and C are integers so they are OK. F is suffixed with 'F' to indicate it is a float so it is OK. Otherwise are considered double?
 
Ja vardhan
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it seems D is wrong because 42e7 is double number (hope its going beyond the float range). So assgining double to float is wrong.

Why the yhave given D is right ???

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic