0% found this document useful (0 votes)
19 views8 pages

Java Mathematical Library Methods

The document provides a series of questions and answers related to Java's Math library methods, covering operations such as finding squares, square roots, absolute values, and rounding functions. It explains the expected outputs of various Math methods and distinguishes between similar functions like Math.ceil and Math.floor, as well as Math.rint and Math.round. Additionally, it includes syntax for common functions and their return data types.

Uploaded by

Hemashree
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)
19 views8 pages

Java Mathematical Library Methods

The document provides a series of questions and answers related to Java's Math library methods, covering operations such as finding squares, square roots, absolute values, and rounding functions. It explains the expected outputs of various Math methods and distinguishes between similar functions like Math.ceil and Math.floor, as well as Math.rint and Math.round. Additionally, it includes syntax for common functions and their return data types.

Uploaded by

Hemashree
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

Mathematical Library Methods

Choose the correct answer

Question 1

Which of the following is false to find square of a number?

1. [Link](a,2)
2. a*a
3. [Link](a,2) ✓
4. All of the above

Question 2

What type of value is returned by [Link]( )?

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. [Link](a) ✓
3. Squareroot(a)
4. None

Question 4

Name the class that is used for different Mathematical functions.

1. [Link] ✓
2. [Link]
3. [Link]
4. None

Question 5

Give the output of the [Link](x); when x = -9.99


1. -9.99
2. 9.99 ✓
3. 0.99
4. None

Question 6

Give the output of [Link](x); when x = 9.0

1. 3
2. 3.0 ✓
3. 3.00
4. all

Predict the output

Question 1

[Link]([Link](10.24));

Output

3.2

Explanation

[Link] method gives the square root of a positive number. Square root of 10.24 is 3.2 so it
is the output.

Question 2

[Link]([Link](-99.4));

Output

-99.0

Explanation

[Link] method rounds off its argument to the nearest mathematical integer and returns its
value as a double type. The nearest integer to -99.4 is -99.0 so that is the output. [Link]
method behaves in a particular way at the mid-point i.e. when the decimal part of the
argument is 0.5. In such cases, the result is the integer value that is even. Let's understand this
with an example. [Link](1.5) and [Link](2.5) will both return 2.0. In the case of 1.5,
both 1.0 and 2.0 are equally close to 1.5. [Link] choses the integer that is even so 2.0 is
returned. In the case of 2.5, both 2.0 and 3.0 are equally close to 2.5. [Link] again choses
the integer that is even so 2.0 is returned.
Question 3

[Link]([Link](42.875));

Output

3.5

Explanation

[Link] 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

[Link]([Link](-25.5, -12.5));

Output

-25.5

Explanation

[Link] method returns the smaller of its 2 arguments. As -25.5 is smaller than -12.5 so it is
the output.

Question 5

[Link]([Link](-0.95));

Output

-0.0

Explanation

[Link] 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

[Link]([Link](-18.51));

Output

-19

Explanation
[Link] method rounds off its argument to the nearest mathematical integer and returns
its value as an int or long type. At the mid-point i.e. when the decimal part of the argument is
0.5, [Link] method rounds up to the higher integer. In this case, the nearest integer to -
18.51 is -19 so it is the output.

Question 7

[Link]([Link](-77.66, -87.45));

Output

-77.66

Explanation

[Link] method returns the greater of its 2 arguments. As -77.66 is greater than -87.45 so it
is the output.

Question 8

[Link]([Link](-0.88));

Output

-1.0

Explanation

[Link] 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

[Link]([Link](98.5));

Output

98.0

Explanation

[Link] method rounds off its argument to the nearest mathematical integer and returns its
value as a double type. This method behaves in a particular way at the mid-point i.e. when the
decimal part of the argument is 0.5. In such cases, the result is the integer value that is even.
Let's understand this with an example. [Link](97.5) and [Link](98.5) will both return
98.0. In the case of 97.5, both 97.0 and 98.0 are equally close to 97.5. [Link] choses the
integer that is even so 98.0 is returned. In the case of 98.5, both 98.0 and 99.0 are equally
close to 98.5. [Link] again choses the integer that is even so 98.0 is returned.
Question 10

[Link]([Link](65.5));

Output

66.0

Explanation

[Link] 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.

Write down the syntax for the following functions

Question 1

To find the smaller between two numbers p and q

Answer

[Link](p, q)

Question 2

To find the absolute value of a number m

Answer

[Link](m)

Question 3

To find the exponent of a number k

Answer

[Link](k)

Question 4

To find the square root of a number d

Answer

[Link](d)

Question 5
To find the rounded-off of a number b

Answer

[Link](b)

Predict the return data type of the following functions

Question 1

[Link]( );

Answer

double

Question 2

[Link]( );

Answer

double

Question 3

[Link]( );

Answer

double

Question 4

[Link]( );

Answer

int or long

Question 5

[Link]( );

Answer

double

Question 6
[Link]( )

Answer

double

Explain the following functions

Question 1

[Link]( )

Answer

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

Question 2

[Link]( )

Answer

Returns the greater of its 2 arguments. Its return type is same as the type of its
arguments.

Question 3

[Link]( )

Answer

Returns the cube root of its argument as a double value.

Question 4

[Link]( )

Answer

Returns the absolute value of its argument. Its return type is same as the type of its
arguments.

Question 5

[Link]( )

Answer

Returns the natural logarithm of its argument. Both return type and argument is of
double data type.
Distinguish between them with suitable examples

Question 1

[Link]( ) and [Link]( )

Answer

[Link]( ) [Link]( )

Returns the smallest double value that is Returns the largest double value that is less
greater than or equal to the argument and is than or equal to the argument and is equal to
equal to a mathematical integer a mathematical integer.

double a = [Link](65.5); double b = [Link](65.5);


In this example, a will be assigned the value In this example, b will be assigned the value
of 66.0 as it is the smallest integer greater of 65.0 as it is the largest integer smaller than
than 65.5. 65.5.

Question 2

[Link]( ) and [Link]( )

Answer

[Link]( ) [Link]( )

Rounds off its argument to the nearest


Rounds off its argument to the nearest
mathematical integer and returns its value as an
mathematical integer and returns its value as a
int or long type. If argument is float, return type
double type.
is int, if argument is double, return type is long.

At mid-point, it returns the integer that is even At mid-point, it returns the higher integer.

long a = [Link](1.5);
double a = [Link](1.5);
long b = [Link](2.5);
double b =[Link](2.5);
a will have a value of 2 and b will have a value
Both, a and b will have a value of 2.0
of 3

Common questions

Powered by AI

The Math.rint method rounds its argument to the nearest mathematical integer and returns it as a double. At midpoints like 0.5, it prefers the even integer. For instance, Math.rint(1.5) and Math.rint(2.5) both return 2.0 because the resulting integer is even . On the other hand, Math.round rounds off to the nearest integer but returns it as an int or long, choosing the higher integer at the midpoint. For example, Math.round(1.5) results in 2, and Math.round(2.5) results in 3 .

Math.ceil returns the smallest integer that is greater than or equal to the given value. For positive decimal numbers, this means rounding up to the nearest whole number that is larger. For example, Math.ceil(65.5) results in 66.0 . For negative decimal numbers, since the integer less negative than the decimal will be more negative, Math.ceil(-0.95) rounds to -0.0 because it's less than zero but more than -1 .

Applying Math.max(p, q) with p = 5.75 and q = -2.3 would return 5.75, as it is the larger numerical value of the two . Math.min(p, q) would return -2.3, reflecting the function's purpose of identifying the smallest number in the given pair. These operations illustrate Java's capability of comparing floating-point numbers to ascertain extreme values in a given range.

Math.floor and Math.ceil yield identical results when applied to whole numbers, as both functions would treat the integer value as their respective results. For instance, Math.floor(4.0) and Math.ceil(4.0) both return 4.0 since there are no fractional parts driving these functions to change the integer . These functions diverge in outputs only when the input presents decimal values, where floor rounds down to the closest integer and ceil rounds up.

Math.pow(a,2) is valid because it raises 'a' to the power of 2, directly calculating the square of 'a'. Conversely, Math.sqrt(a,2) is invalid because Math.sqrt only takes a single argument and calculates the square root of that number; it doesn't support additional arguments to specify any other type of root calculation. Due to this incorrect syntax, the tool doesn't work for obtaining the square .

Math.random() could be used to generate random numbers for a game in which a player simulates rolling a die. The range of Math.random() is between 0.0 inclusive and 1.0 exclusive. By generating a value, multiplying it by 6, and casting it to an int, you could produce a pseudo-random integer between 0 and 5. Adding 1 to result transforms it into a realistic dice roll value between 1 and 6 . This approach ensures the number respects the game's requirements while harnessing the function's inherent range.

Math.rint requires a double type input and rounds to the nearest integer, specifically favoring the even integer at .5 midpoints—for example, Math.rint(1.5) results in 2.0 . In contrast, Math.round with a float argument returns an int and rounds up at the midpoint, making Math.round(1.5f) yield 2 due to int type precision . Their distinct rounding preferences and return types underline Java's strategic variance in processing different floating-point numbers.

Math.sqrt() calculates the square root of a number and returns it as a double. For instance, Math.sqrt(9.0) results in 3.0 . Conversely, Math.cbrt() calculates the cube root, returning a double as well. For example, Math.cbrt(42.875) yields 3.5 . Despite having similar roles of extracting roots, the mathematical operations differ inherently due to the powers involved—exponent 2 for square root and exponent 3 for cube root—hence differing outputs for similar inputs.

Math.abs returns the absolute value of an input number, preserving the input's type. This approach supports flexibility and compatibility across numerical types in Java, ensuring that the data property (like precision in float or double) remains consistent through operations. For instance, the absolute value of -9.99 would be 9.99, and stored in double, securing the precision of the floating-point number .

A developer might prefer Math.exp(k) due to its precision and efficiency in computational execution, which are optimized for determining e raised to the power of k (where e is the base of natural logarithms approximately equal to 2.718). This choice mitigates errors that could arise from manually defining e and using multiplication or pow functions, which might introduce minute inaccuracies . Leveraging Math.exp offers a standardized approach that enhances code maintainability and reliability.

You might also like