Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jtips mock1 #14

 
Hung Chang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need help with this question.
14. Given:
class Q14 {
public static void main(String[] args) {
int intNumber = 123456789;
float floatNumber = intNumber;
int x = (int)floatNumber;
System.out.println(intNumber - x);
System.out.println(intNumber == x );
}
}
Choose 2 answers.
1.Prints 0 and true .
2.Prints 0 and false.
3.Prints (some number due to loss of precision) and true.
4.Prints some integer due to loss of precision and false.
when it compiles, it prints ans 4.. but when i change intNumber =2147483648 or other numbers, it compiles and prints ans 1.
can anyone explain it to me plz.. thanx in advance
 
Argm Mastoi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
This program is always printing option 4. whether i changed the values also.
i used this code to see what actually is going on :
class Q14 {
public static void main(String[] args) {
int intNumber = 213456789;
float floatNumber = intNumber;
int x = (int)floatNumber;
System.out.println("floatNumber " +floatNumber);
System.out.println("intNumber" +intNumber);
System.out.println("x" +x);
System.out.println(intNumber - x);
System.out.println(intNumber == x );
}
}
 
Hung Chang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,Argm
it doesnt always print ans4.
if i pick x=5.. it prints ans4
 
Hung Chang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry.. ans1
 
Argm Mastoi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have u checked that what's actually going on inside, the way i told you
 
Argm Mastoi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also this ans4 is for this perticular value given in the mock not for all values :-)
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just as Argm did, I put in a few extra print lines to see what was actually going on. When the intNumber is cast up to a float there is a slight loss of precision. floatNumber actually become 1.23456792E8 when assigned the cast value. When it's cast back to int x, x becomes 123456792. When you subtract these numbers, it does not give 0, and the second part is false. It is because of this loss of precision from casting from intNumber up to floatNumber that the answer is not exactly what you may expect it to be.
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
 
Akshayan Venkatesh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hung Chang:
i need help with this question.
14. Given:

class Q14 {
public static void main(String[] args) {
int intNumber = 123456789;
float floatNumber = intNumber;
int x = (int)floatNumber;
System.out.println(intNumber - x);
System.out.println(intNumber == x );
}
}
Choose 2 answers.
1.Prints 0 and true .
2.Prints 0 and false.
3.Prints (some number due to loss of precision) and true.
4.Prints some integer due to loss of precision and false.

when it compiles, it prints ans 4.. but when i change intNumber =2147483648 or other numbers, it compiles and prints ans 1.
can anyone explain it to me plz.. thanx in advance
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic