Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from enthuware mock exam

 
Ed Cardenas
Ranch Hand
Posts: 43
1
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers!

I was doing enthuware mock exams and came across this question:


Given the following LOCs:    
int rate = 10;    
XXX amount = 1 - rate/100*1 - rate/100;
What can XXX be?

* only int or long
* only long or double
* only double
* double or float
* long or double but not int or float.
* int, long, float or double


enthuware correct answer: int, long, float or double



Enthuware's e explanation was that "none of the terms in the expression 1 - rate/100*1 - rate/100; is double or float".
Yeah, their right that the result is not double or float but I am just not satisfied with their explanation (although the result
of the expression after compiling is 1), based on my manual calculation (basing on operator precedence) the result of the
expression should have been this:

amount = 1 - rate/100*1 - rate/100;
amount = 1 - 10/100*1 - 10/100;
amount = 1 - 0.1*1 - 10/100;
amount = 1 - 0.1 - 0.1;
amount = 0.9 - 0.1;
amount = 0.8;

Can someone explain to me further? what have I done wrong?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3819
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is just one of the rules of the java language that to get a double out of any expression at least one of the terms has to be a double or float. So 10/100 will not give you 0.1. It will give you 0. If you had 10.0/100, then you would get 0.1. In other words, if all the operands on an operator are int, or byte or short or char (i.e. no operand is a float or a double), an integral operation will be performed and an int will be returned.

Therefore, you should see it like this:
amount = 1 - rate/100*1 - rate/100;
amount = 1 - 10/100*1 - 10/100;
amount = 1 - 0*1 - 0;
amount = 1 - 0 - 0;
amount = 1;


You may want to read more here: http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.2
HTH,
Paul.
Added Later:
In case someone sees this thread, the complete explanation provided with the question is quoted below:

Note that none of the terms in the expression 1 - rate/100*1 - rate/100; is double or float. They are all ints. So the result of the expression will be an int. Since an int can be assigned to a variable of type int, long, float or double, amount can be int, long, float or double.
 
Ed Cardenas
Ranch Hand
Posts: 43
1
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fantastic!

Now I know now, thanks Paul for the detailed explanation!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ed,

As you already discovered (thanks to Paul's detailed explanation), it's actually (a bit) related with one of your other questions. Only integers, so the type of the calculation is int. And an int can fit into a long, double and float too.

So with the recently acquired knowledge what would be the correct answer if the question was changed to:
Given the following LOCs:
int rate = 10;
XXX amount = 1 - rate/100.0*1 - rate/100;
What can XXX be?


Kind regards,
Roel
 
Ed Cardenas
Ranch Hand
Posts: 43
1
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
So with the recently acquired knowledge what would be the correct answer if the question was changed to:
Given the following LOCs:
int rate = 10;
XXX amount = 1 - rate/100.0*1 - rate/100;
What can XXX be?



Hi Roel,

Just to make sure, I reviewed my previous thread because I forgot it already.

My answer: Only double, because the compiler thinks that a double literal value can only fit in a primitive type.
Primitive types int, long, and float can't hold a double literal. These need explicit casting to compile.

Is that right?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ed Cardenas wrote:My answer: Only double, because the compiler thinks that a double literal value can only fit in a primitive type.
Primitive types int, long, and float can't hold a double literal. These need explicit casting to compile.

Is that right?

From the OCAJP7 perspective it's 100% correct A double literal value is used, so calculations will result in a double value. And a double is the only primitive type that can hold a double without an explicit cast.

(Note: from an OCPJP7 point-of-view the statement a double literal value can only fit in a primitive type is not 100% correct, because there are also wrapper classes; each primitive type has its own wrapper class. And via a language feature called "boxing" a primitive type can be converted automatically into an instance of its wrapper class, and for double that's Double (with capital D. But as I said, no need to worry about wrapper classes for the OCAJP7 This note is just for completeness.)
 
Ed Cardenas
Ranch Hand
Posts: 43
1
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whew! I knew it.

Thanks for the extra note. These brain twisters are really big help for preparing the exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic