[go: up one dir, main page]

0% found this document useful (0 votes)
15 views6 pages

Chapter 6

Chapter 6 covers various mathematical library methods in Java, including functions for square roots, absolute values, and logarithms. It includes multiple-choice questions, predictions of output for specific code snippets, and explanations of functions like Math.random() and Math.max(). Additionally, it provides examples of Java programs utilizing these mathematical functions.

Uploaded by

pragathipug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Chapter 6

Chapter 6 covers various mathematical library methods in Java, including functions for square roots, absolute values, and logarithms. It includes multiple-choice questions, predictions of output for specific code snippets, and explanations of functions like Math.random() and Math.max(). Additionally, it provides examples of Java programs utilizing these mathematical functions.

Uploaded by

pragathipug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Chapter 6

Mathematical Library Methods


I Choose the correct answer
Question 1
Which of the following does not find the square of a number?
1. Math.pow(a,2)
2. a*a
3. Math.sqrt(a,2) ✓
4. All of the above
Question 2
What type of value is returned by Math.sqrt( )?
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
Name the class that is used for different Mathematical functions.
1. Java.Math ✓
2. Java.Power
3. Java.Sqrt
4. None
Question 5
Give the output of the Math.abs(x); when x = -9.99
1. -9.99
2. 9.99 ✓
3. 0.99
4. None
Question 6
Give the output of Math.sqrt(x); when x = 9.0
1. 3
2. 3.0 ✓
3. 3.00
4. all
II. Predict the output
Question 1
System.out.println(Math.sqrt(10.24));
Output
3.2
Question 2
System.out.println(Math.rint(-99.4));
Output
-99.0
Question 3
System.out.println(Math.cbrt(42.875));
Output
3.5
Question 4
System.out.println(Math.min(-25.5, -12.5));
Output
-25.5
Question 5
System.out.println(Math.ceil(-0.95));
Output
-0.0
Question 6
System.out.println(Math.round(-18.51));
Output
-19
Question 7
System.out.println(Math.max(-77.66, -87.45));
Output
-77.66
Question 8
System.out.println(Math.floor(-0.88));
Output
-1.0
Question 9
System.out.println(Math.rint(98.5));
Output
98.0
Question 10
System.out.println(Math.ceil(65.5));
Output
66.0
III. Write down the syntax for the following functions
Question 1
To find the smaller between two numbers p and q
Answer
Math.min(p, q)
Question 2
To find the absolute value of a number m
Answer
Math.abs(m)
Question 3
To find the exponent of a number k
Answer Math.exp(k)
Question 4
To find the square root of a number d
Answer Math.sqrt(d)
Question 5
To find the rounded-off of a number b
Answer
Math.round(b)
IV. Predict the return data type of the following functions
Question 1
Math.sqrt( );
Answer
double
Question 2
Math.rint( );
Answer
double
Question 3
Math.ceil( );
Answer
double
Question 4
Math.round( );
Answer
int or long
Question 5
Math.floor( );
Answer
double
Question 6
Math.log( )
Answer
double
V. Explain the following functions
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
Returns the cube root of its argument as a double value.
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.
VI. Distinguish between them with suitable examples
Question 1
Math.ceil( ) and Math.floor( )
Answer
Math.ceil( ) Math.floor( )
Returns the smallest double value that is greater than Returns the largest double value that is less
or equal to the argument and is equal to a than or equal to the argument and is equal to a
mathematical integer mathematical integer.
double b = Math.floor(65.5);
double a = Math.ceil(65.5);
In this example, b will be assigned the value of
In this example, a will be assigned the value of 66.0
65.0 as it is the largest integer smaller than
as it is the smallest integer greater than 65.5.
65.5.
Question 2
Math.rint( ) and Math.round( )
Answer
Math.rint( ) Math.round( )
Rounds off its argument to the nearest mathematical
Rounds off its argument to the nearest
integer and returns its value as an int or long type. If
mathematical integer and returns its
argument is float, return type is int, if argument is double,
value as a double type.
return type is long.
At mid-point, it returns the integer that
At mid-point, it returns the higher integer.
is even
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 value of 2.0 a will have a value of 2 and b will have a value of 3

VII. Unsolved Programs


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;

public class GreatestNumber


{
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 g = Math.max(a, b);


g = Math.max(g, c);

int s = Math.min(a, b);


s = Math.min(s, c);

System.out.println("Greatest Number = " + g);


System.out.println("Smallest Number = " + s);
}
}
Question 2
Write a program in Java to calculate and display the hypotenuse of a Right-Angled Triangle by
taking perpendicular and base as inputs.
Hint: h = √p2 + b2
import java.util.Scanner;

public class Hypotenuse


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Perpendicular: ");
double p = in.nextDouble();
System.out.print("Enter Base: ");
double b = in.nextDouble();

double h = Math.sqrt(p*p+b*b);

System.out.println("Hypotenuse = " + h);


}
}
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;

public class MathMethods


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Number: ");
double n = in.nextDouble();
System.out.println("Natural logarithm = " + Math.log(n));
System.out.println("Absolute value = " + Math.abs(n));
System.out.println("Square root = " + Math.sqrt(n));
System.out.println("Cube root= " + Math.cbrt(n));
System.out.println("Random number = " + Math.random());
}
}
Question 4
In an examination, you have appeared for three subjects i.e. Physics, Chemistry and Biology.
Write a program in Java to calculate the average mark obtained and finally display the marks in
rounded-off form.
Take Physics, Chemistry. and Biology marks as inputs.
import java.util.Scanner;

public class AvgMarks


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter Marks");
System.out.print("Physics: ");
int p = in.nextInt();
System.out.print("Chemistry: ");
int c = in.nextInt();
System.out.print("Biology: ");
int b = in.nextInt();

double avg = (p + c + b) / 3.0;


long roundAvg = Math.round(avg);

System.out.println("Rounded Off Avg Marks = " + roundAvg);


}
}
Question 5
You want to calculate the radius of a circle by using the formula:
Area = (22/7) * r2; where r = radius of a circle
Hence the radius can be calculated as:
r = √((7 * area) / 22)
Write a program in Java to calculate and display the radius of a circle by taking area as an input.
import java.util.Scanner;

public class CircleRadius


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Area of Circle: ");
double area = in.nextDouble();
double r = Math.sqrt((7 * area )/ 22.0);
System.out.print("Radius of Circle = " + r);
}
}
**********

You might also like