In: Computer Science
java code
Question 2: What are the type and value of each of the expressions below?
int [] numbers = { 3, 6, 15, 22, 100, 0 };
double [] decimals = { 3.5, 4.5, 2.0, 2.0, 2.0 };
String [] words = {"alpha", "beta", "gamma"};
numbers[3] + numbers[2]
decimals[2] - decimals[0] + numbers[4]
words[1].charAt( numbers [0] )
numbers[4] * decimals[1] <= numbers[5] * numbers[0]
There are a total of 4 expressions which are -
1) numbers[3] + numbers[2]
Now since numbers is of integer type hence any operation on it will remains the same type. So the type of this expression is integer or int.
Now since index starts from 0 so,
Numbers[3] wil be 22 and numbers[2] will be 12 os the output will be -
22 +12 = 34
2) decimals[2] - decimals[0] + numbers[4]
As explained in the above part its data type will also be same. The type will be double. The output of expression will be -
2.0 - 3.5 +2.0 = 0.5
3) words[1].charAt( numbers [0] )
The type of this will be string. The output of. Numbers[0] will be 3 so the equation will become -
words[1].charAt(3) now words[1] is equal to beta. So the equation will be -
'beta'. CharAt(3)= a. - #the character at 3rd index.
4) numbers[4] * decimals[1] <= numbers[5] * numbers[0]
The type of this expression will be boolean as it'll return either true or false. As its a comparison.
The values of variables are -
Numbers[4] =100
Numbers[5] = 0
Numbers[0] =3
Decimals[1] = 4.5
So the equation will be as -
100*4.5 <= 0*3. - - > 450 <= 0
Which is false as 450 > 0. So the answer will be false.