import cs1.Keyboard;
public class Questions
{
public static void main(String[ ] args)
{
int x, y, z;
double average;
System.out.println("Enter an integer value");
x = Keyboard.readInt( );
System.out.println("Enter another integer value");
y = Keyboard.readInt( );
System.out.println("Enter a third integer value");
z = Keyboard.readInt( );
average = (x + y + z) / 3;
System.out.println("The result of my calculation is " + average);
}
}
1) Questions computes
a) The correct average of x, y and z as a double
b) The correct average of x, y and z as an int
c) The average of x, y and z as a double, but the result may not be accurate
d) the sum of x, y and z
e) the remainder of the sum of x, y and z divided by 3
Answer: c. Explanation: Because the division is an int division, even though the result is stored in a double, the resulting double may not be accurate. For instance, if x, y and z are 1, 2 and 4, the double average should be 2.33333 but average will instead be 2.00000.
2) What is output if x = 0, y = 1 and z = 1?
a) 0
b) 0.0
c) 0.6666666666666666
d) 0.6666666666666667
e) 0.67
Answer: b. Explanation: The division is performed as an int division since x, y, z and 3 are all ints.
Therefore, average gets the value 0.0. It is output as 0.0 instead of 0 because average is a double, which outputs at least one decimal digit unless specified otherwise using the DecimalFormat class.