Arithmetic Operators

Correct use of data types for different arithmetic operations.

This exercise will help write a program that calculates the average with decimal points

Question 1

public static void main(String[] args) {
    //This program calculates the average with decimal points
    int gradeOne = 89;
    int gradeTwo = 92;
    int gradeThree = 95;
    //Calculate the average grades of three midterms by dividing the sum with 3, and stored the result into a variable
    int averageOne = (gradeOne + gradeTwo + gradeThree) / 3;
}

True or False: The code calculates the average of three numbers correctly:

a.) True

b.) False

Question 2

public static void main(String[] args) {
    //This program calculates the average with decimal points
    int gradeOne = 89;
    int gradeTwo = 92;
    int gradeThree = 95;
    //Calculate the average by dividing the sum with 3, and stored the result into a double variable
    double averageTwo = (gradeOne + gradeTwo + gradeThree) / 3;
}

Which one of the following is true about the given section of code?

a.) averageTwo is calculated correctly

b.) averageTwo is not calculated with decimal points

c.) Code will give error because an int value cannot be assigned to a double identifier

Question 3

public static void main(String[] args) {
    //This program calculates the average with decimal points
    int gradeOne = 82;
    int gradeTwo = 92;
    int gradeThree = 95;
    //Calculate the average by dividing the sum with 3.0, and stored the result into a double variable
    double averageThree = (gradeOne + gradeTwo + gradeThree) / 3.0;
}

True or False: The code calculates the average of three numbers correctly

a.) True

b.) False

Question 4

public static void main(String[] args) {
    //This program calculates the average with decimal points
    int gradeOne = 82;
    int gradeTwo = 92;
    int gradeThree = 95;
    //Calculate the average by dividing the sum (using double casting) with 3, and stored the result into a double variable
    double averageFour = (double)((gradeOne + gradeTwo + gradeThree) / 3); 
}

Which one of the following is true about the given section of code?

a.) averageFour is calculated correctly with decimal points

b.) The double cast is in the wrong place

c.) gradeOne, gradeTwo and gradeThree should be declared as double