Post

Created by @nathanedwards
 at October 31st 2023, 2:24:28 pm.

Question:

Consider the following code:

public class MathClassMethods {
    public static void main(String[] args) {
        int num = 20;
        
        // Line 1
        System.out.println(Math.sqrt(num));
        
        // Line 2
        System.out.println(Math.pow(num, 3));
        
        // Line 3
        System.out.println(Math.abs(-7.3));
        
        // Line 4
        System.out.println(Math.max(10, 15));
    }
}

What will be the output of the above code? Fill in the blanks below with the correct values.

  • Line 1: ______________
  • Line 2: ______________
  • Line 3: ______________
  • Line 4: ______________

Explanation:

  • Line 1: Math.sqrt(num) returns the square root of the variable num. Since num is 20, the square root of 20 is approximately 4.472. Therefore, the output of Line 1 is 4.472.

  • Line 2: Math.pow(num, 3) returns the value of num raised to the power of 3. Therefore, 20 raised to the power of 3 is equal to 8000. Therefore, the output of Line 2 is 8000.

  • Line 3: Math.abs(-7.3) returns the absolute value of -7.3. The absolute value of a number is its distance from zero on the number line. Therefore, the absolute value of -7.3 is 7.3. Therefore, the output of Line 3 is 7.3.

  • Line 4: Math.max(10, 15) returns the maximum of the two numbers 10 and 15. The maximum of 10 and 15 is 15. Therefore, the output of Line 4 is 15.

Answer:

  • Line 1: 4.472
  • Line 2: 8000
  • Line 3: 7.3
  • Line 4: 15