What does the variable x contain at the end of each of these computations? If th
ID: 3549741 • Letter: W
Question
What does the variable x contain at the end of each of these computations? If the code is not legal in Java, say so.
int iCount = 19;
double dCount = 4.3;
int iSize = 3;
double dSize = 2.7;
a. int x = iCount + iSize;
b. int x = (int) dCount + iCount;
c. double x = (int) dCount - iCount;
d. int x = (int) dCount + - dSize;
e. int x = iCount % iSize;
f. double x = iCount / iSize;
g. int x = iSize * iSize * iSize / iCount % iSize;
h. double x = dSize + iCount - dCount + -dSize;
i. double x = iSize - iCount + iSize / dCount;
j. double x = ((iCount - iSize) + iCount) - (dCount * iSize)/dSize;
PLEASE EXPLAIN!!
Explanation / Answer
explanation
here, int iCount = 19;
double dCount = 4.3;
int iSize = 3;
double dSize = 2.7;
Lets go one by one
a. int x = iCount + iSize;
here, iCount = 19, iSize = 3, so 19 + 3 = 22
b. int x = (int) dCount + iCount;
the value of dCount is 4.3, but when the double value is converted to int, 4.3 is changed to 4.
and value of iCount is 19
so 4 + 19 = 23
c. double x = (int) dCount - iCount;
the value of dCount is 4.3, but when the double value is converted to int, 4.3 is changed to 4.
value of iCount is 19
so 4-19 = -15 (integer)
the value when converted into double becomes -15.0
d. int x = (int) dCount + - dSize;
the value of dCount is 4.3, but when the double value is converted to int, 4.3 is changed to 4.
value of 2.7 so
4+-2.7. the return type is of int type as type of x is integer, but if we subtract this, we get decimal value which is of double type. So it ll throw run time error
e. int x = iCount % iSize;
value of iCount is 19
value of iSize is 3
19%3 is 1
f. double x = iCount / iSize;
value of iCount is 19
value of iSize is 3
19/3 = 6.166. but as both the values are of int type and not double, so it ll be 6.
now 6 is converted to double as x is of double type, so the answer is 6.0
g. int x = iSize * iSize * iSize / iCount % iSize;
all three have same precedece in java and left precedence
equation is 3*3*3/19%3
27/19%3
27/19 (is 1.something but as both are of integer type, decimal ll be removed, and it ll be just 1
1%3 =1
h. double x = dSize + iCount - dCount + -dSize;
dSize = 2.7
iCount = 19
dCount = 4.3
2.7 + 19 - 4.3 +-2.7
= 14.7
i. double x = iSize - iCount + iSize / dCount;
Here / sign has the highest preference
iSize/dCount = 3/4.3 = 0.6976
now the equation is iSize - iCount +0.6976
3-19 +0.6976
which is equal to -15.30232558139535
j. double x = ((iCount - iSize) + iCount) - (dCount * iSize)/dSize;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.