Mathematical Library Methods

Solutions for Computer Applications, Class 9, ICSE

Assignment Questions

8 questions

Question 1

Distinguish between Math.ceil() and Math.floor() methods.

Assignment Questions

Answer:

Math.ceil( )Math.floor( )
Returns the smallest double value that is greater than or equal to the argument and is equal to a mathematical integerReturns the largest double value that is less than or equal to the argument and is equal to a mathematical integer.
double a = Math.ceil(65.5);
In this example, a will be assigned the value of 66.0 as it is the smallest integer greater than 65.5.
double b = Math.floor(65.5);
In this example, b will be assigned the value of 65.0 as it is the largest integer smaller than 65.5.

Question 2

Explain the following Math functions in Java:

i. Math.sqrt()

Assignment Questions

Answer:

Returns the square root of its argument as a double value. For example, Math.sqrt(25) will return 5.0.

ii. Math.cbrt()

Returns the cube root of its argument as a double value. For example, Math.cbrt(27) will return 3.0.

iii. Math.random()

Returns a positive double value, greater than or equal to 0.0 and less than 1.0.

iv. Math.round()

Rounds off its argument to the nearest mathematical integer and returns its value as an int or long type. If argument is float, return type is int, if argument is double, return type is long. At mid-point, it returns the higher integer. For example, Math.round(2.5) will return 3.

Question 3

Write the following as Java expressions:

i. x3y22xy\lvert x^3 - y^2 -2xy\rvert

Assignment Questions

Answer:

Math.abs(Math.pow(x, 3) - Math.pow(y, 2) - 2*x*y)

ii. π6(z42π)\dfrac{\pi}{6}(z^4 - 2\pi)

Math.PI / 6*(Math.pow(z, 4) - 2*Math.PI)

iii. z2π3\sqrt[3]{z^2 - \pi}

Math.cbrt(z*z - Math.PI)

iv. x3y34\sqrt[4]{x^3 - y^3}

Math.pow(x*x*x - y*y*y, 1/4)

v. amountrate11(1+rate)n\cfrac{amount*rate}{1 - \cfrac{1}{(1 + rate)^n}}

(amount*rate) / (1 - (1 / Math.pow(1+rate, n)))

Question 4

Write valid statements for the following in Java:

i. Print the rounded off value of 234.49

Assignment Questions

Answer:

System.out.println(Math.round(234.49));

ii. Print the absolute value of -9.99

System.out.println(Math.abs(-9.99));

iii. Print the largest of -45 and -50

System.out.println(Math.max(-45, -50));

iv. Print the smallest of -56 and -57.4

System.out.println(Math.min(-56, -57.4));

v. Print a random integer between 50 and 70

int range = 70 - 50 + 1;
int num = (int)(range * Math.random() + 50);
System.out.println(num);

vi. Print 10.5 raised to the power 3.8

System.out.println(Math.pow(10.5, 3.8));

Question 5

Write a program in Java to find the minimum of three numbers using Math.min() method.

Assignment Questions

Answer:

import java.util.Scanner;

public class KboatSmallestNumber
{
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter First Number: ");
        int a = in.nextInt();
        System.out.print("Enter Second Number: ");
        int b = in.nextInt();
        System.out.print("Enter Third Number: ");
        int c = in.nextInt();
        
        int s = Math.min(a, b);
        s = Math.min(s, c);
        
        System.out.println("Smallest Number = " + s);
    }
}
Output
BlueJ output of KboatSmallestNumber.java

Question 6

Write a program to print:

i. p to the power q
ii. the square root of p

The values of p and q are 64 and 2 respectively.

Assignment Questions

Answer:

public class KboatNumber
{
    public static void main(String args[]) {
        int p = 64;
        int q = 2;
        
        double r1 = Math.pow(p, q);
        System.out.println("p to the power of q = " + r1);
        
        double r2 = Math.sqrt(p);
        System.out.println("Square root of p = " + r2);
    }
}
Output
BlueJ output of KboatNumber.java

Question 7

Write a program to generate random integers in the range 10 to 20.

Assignment Questions

Answer:

public class KboatRandom
{
    public static void main(String args[]) {
        int min = 10, max = 20;
        int range = max - min + 1;
        int num = (int)(range * Math.random() + min);
        System.out.println("Random number in the range 10 to 20: " + num);
    }
}
Output
BlueJ output of KboatRandom.java

Question 8

Write the equivalent Java statements for the following, only using the mathematical functions:

i. Print the positive value of -101.

Assignment Questions

Answer:

System.out.println(Math.abs(-101));

ii. Store the value -125 in a variable and print its cube root.

int a = -125;
System.out.println(Math.cbrt(a));

iii. Store the value 89.99 in a variable and convert it into its closest integer that is greater than or equal to 89.99.

double a = 89.99;
System.out.println(Math.round(a));

Multiple Choice Questions

8 questions

Question 1

Which Java package includes the Math class?

  1. java.io
  2. java.lang ✓
  3. java.util
  4. java.sys
Multiple Choice Questions

Answer:

Question 2

Give the output of Math.sqrt(x); when x = 9.0

  1. 3
  2. 3.0 ✓
  3. 3.00
  4. All of these
Multiple Choice Questions

Answer:

Question 3

Give the output of Math.ceil(46.6).

  1. 46.6
  2. 46.5
  3. 47.0 ✓
  4. 46.0
Multiple Choice Questions

Answer:

Question 4

Give the output of Math.abs(x); when x = -9.99

  1. -9.99
  2. 9.99 ✓
  3. 0.99
  4. None of these
Multiple Choice Questions

Answer:

Question 5

Which of the following is a method to find the square root of a number?

  1. FindSquareroot(x)
  2. Sqrt(x)
  3. Math.Square(x)
  4. Math.sqrt(x) ✓
Multiple Choice Questions

Answer:

Question 6

What will be the output of Math.pow(2, 0)?

  1. 0.0
  2. 1.0 ✓
  3. 2.0
  4. -1.0
Multiple Choice Questions

Answer:

Question 7

Math.random() returns a double value r such that

  1. 0.0 <= r < 1.0 ✓
  2. 0.0 <= r <= 1.0
  3. 0.0 < r <= 1.0
  4. 0.0 < r < 1.0
Multiple Choice Questions

Answer:

Question 8

Give the output of Math.floor(46.6)?

  1. 46.5
  2. 47.0
  3. -46.0
  4. 46.0 ✓
Multiple Choice Questions

Answer: