In: Computer Science
If x=3, y=4 what is the value of this Java expression?
(double) (x/y)
What is the value of this Java expression?
(int)(1.0 / 2.0) + (double)(1 / 2)
True or False:
(double)(x)/y is the same as double (x/y)
What is the value of this Java expression?
(double)3
Q1
If x=3, y=4 what is the value of this Java expression?
(double) (x/y)
The value will be 0.0 because first the expression x/y is evaluated as integer then 3/4 because x and y both are integers there will be no upcasting. Hence it will be equal to 0
Q2
What is the value of this Java expression?
(int)(1.0 / 2.0) + (double)(1 / 2)
Similar to above the value will be 0+ 0.0 = 0 because the 1.0 and 2.0 both are floats and the result will be 0.5 which is downcasted to int and results in 0 and 1/2 also becomes 0 because of integer division
Q3
(double)(x)/y is the same as double (x/y)
False as typecasting will be done before division in first case and in the second case after the division which can lead to different results as in case of x=1 and y=2
First will yield 0.5 and second will yield 0
Q4
What is the value of this Java expression?
(double)3
3.0 as it is typecasted to a double