Chapter 6 Mathematical Library Methods Solution+
Chapter 6 Mathematical Library Methods Solution+
Question 1
1. Math.pow(a,2)
2. a*a
3. Math.sqrt(a,2) ✓
4. All of the above
Question 2
1. int
2. float
3. double ✓
4. All
Question 3
Which of the following syntax is true to find the square root of a number?
1. sqrt(a)
2. Math.sqrt(a) ✓
3. Squareroot(a)
4. None
Question 4
1. Java.Math ✓
2. Java.Power
3. Java.Sqrt
4. None
Question 5
1. -9.99
2. 9.99 ✓
3. 0.99
4. None
Question 6
1. 3
2. 3.0 ✓
3. 3.00
4. all
Question 1
System.out.println(Math.sqrt(10.24));
Output
3.2
Explanation
Math.sqrt method gives the square root of a positive number. Square root
of 10.24 is 3.2 so it is the output.
Question 2
System.out.println(Math.rint(-99.4));
Output
-99.0
Explanation
Question 3
System.out.println(Math.cbrt(42.875));
Output
3.5
Explanation
Math.cbrt method returns the cube root of its argument as a double value.
Cube root of 42.875 is 3.5 so it is the output.
Question 4
System.out.println(Math.min(-25.5, -12.5));
Output
-25.5
Explanation
Question 5
System.out.println(Math.ceil(-0.95));
Output
-0.0
Explanation
Math.ceil method returns the smallest double value that is greater than or
equal to the argument and is equal to a mathematical integer. If the
argument value is less than zero but greater than -1.0, then the result is
negative zero which is the case in this question.
Question 6
System.out.println(Math.round(-18.51));
Output
-19
Explanation
Question 7
System.out.println(Math.max(-77.66, -87.45));
Output
-77.66
Explanation
Question 8
System.out.println(Math.floor(-0.88));
Output
-1.0
Explanation
Math.floor method returns the largest double value that is less than or
equal to the argument and is equal to a mathematical integer. As -1.0 is the
largest mathematical integer less than -0.88 so it is the output.
Question 9
System.out.println(Math.rint(98.5));
Output
98.0
Explanation
Question 10
System.out.println(Math.ceil(65.5));
Output
66.0
Explanation
Math.ceil method returns the smallest double value that is greater than or
equal to the argument and is equal to a mathematical integer. Here 66.0 is
the smallest mathematical integer greater than 65.5 so it is the output.
Question 1
Math.random( )
Answer
Returns a positive double value, greater than or equal to 0.0 and less
than 1.0.
Question 2
Math.max( )
Answer
Returns the greater of its 2 arguments. Its return type is same as the
type of its arguments.
Question 3
Math.cbrt( )
Answer
Question 4
Math.abs( )
Answer
Returns the absolute value of its argument. Its return type is same as
the type of its arguments.
Question 5
Math.log( )
Answer
Returns the natural logarithm of its argument. Both return type and
argument is of double data type.
Question 1
Math.ceil( ) Math.floor( )
Returns the smallest double value that is Returns the largest double value that is
greater than or equal to the argument and less than or equal to the argument and is
is equal to a mathematical integer equal to a mathematical integer.
Question 2
Answer
Math.rint( ) Math.round( )
Rounds off its argument to Rounds off its argument to the nearest mathematical
the nearest mathematical integer and returns its value as an int or long type. If
integer and returns its value argument is float, return type is int, if argument is
as a double type. double, return type is long.
double a = Math.rint(1.5);
long a = Math.round(1.5);
double b =Math.rint(2.5);
long b = Math.round(2.5);
Both, a and b will have a
a will have a value of 2 and b will have a value of 3
value of 2.0
Question 1
Write a program in Java to input three numbers and display the greatest
and the smallest of the two numbers.
Hint: Use Math.min( ) and Math.max( )
Sample Input: 87, 65, 34
Sample Output: Greatest Number 87
Smallest number 34
import java.util.Scanner;
Output
Question 2
import java.util.Scanner;
Output
Question 3
Write a program to input a number and evaluate the results based on the
number entered by the user:
(a) Natural logarithm of the number
(b) Absolute value of the number
(c) Square root of the number
(d) Cube of the number
(e) Random numbers between 0 (zero) and 1 (one).
import java.util.Scanner;
Output
Question 4
import java.util.Scanner;
Output
Question 5
Output