posted 3 years ago

Hi Ranchers!

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

enthuware correct answer:

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?

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 doubleint 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

Ranch Hand

Posts: 4105

34

posted 3 years ago

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:

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.

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications

Quality Guaranteed - Pass or Full Refund!

posted 3 years ago

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:

Kind regards,

Roel

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?int rate = 10;

XXX amount = 1 - rate/100.0*1 - rate/100;

What can XXX be?

Kind regards,

Roel

posted 3 years ago

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 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?

posted 3 years ago

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

- 2

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.)