0% found this document useful (0 votes)
29 views5 pages

Java Math Methods Practice Worksheet

The document is a practice worksheet focused on mathematical library methods and Java expressions, containing multiple choice questions and short answer sections. It covers various Math methods in Java, including operations like power, square root, rounding, and random number generation. Additionally, it includes tasks to find outputs, fix errors in code, and evaluate expressions using Math functions.

Uploaded by

Nive Droid
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)
29 views5 pages

Java Math Methods Practice Worksheet

The document is a practice worksheet focused on mathematical library methods and Java expressions, containing multiple choice questions and short answer sections. It covers various Math methods in Java, including operations like power, square root, rounding, and random number generation. Additionally, it includes tasks to find outputs, fix errors in code, and evaluate expressions using Math functions.

Uploaded by

Nive Droid
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

Practice Worksheet – Mathematical Library Methods & Java Expressions

SECTION A – Multiple Choice Questions

1. [Link](2, 5) returns:

a) 10 b) 32 c) 25 d) 64

2. [Link](144) gives:

a) 11 b) 12 c) 72 d) 14

3. Which method finds the cube root of a value?

a) cube() b) root() c) cbrt() d) pow()

4. [Link](3.4) gives:

a) 3 b) 3.0 c) 4.0 d) 4

5. [Link](9.7) is:

a) 10 b) 9.0 c) 9 d) 8

6. [Link](5.6) gives:

a) 6 b) 5 c) 5.0 d) 6.0

7. Which method returns the closest integer as a double?

a) round() b) floor() c) ceil() d) rint()

8. [Link](4.5) returns:

a) 5.0 b) 4.0 c) 4.5 d) 6.0

9. [Link](-25) gives:

a) -25 b) 25 c) 0 d) Error
10. [Link](20, -10) returns:

a) 20 b) -10 c) 10 d) 0

11. [Link]() returns a:

a) double between 0 and 1

b) double between 1 and 10

c) random integer

d) boolean value

12. The package automatically loaded is:

a) [Link] b) [Link] c) [Link] d) [Link]

13. [Link](125) returns:

a) 2 b) 3 c) 4 d) 5

14. Which of the following gives an integer result?

a) round() b) ceil() c) floor() d) rint()

15. (int)([Link]()*6) generates integers from:

a) 1–6 b) 0–5 c) 2–7 d) 0–6

16. [Link](-2.5) returns:


a) -2.5 b) -3.0 c) -2.0 d) -1.0

17. Which has the highest precedence?


a) + b) * c) % d) ()

18. (int)([Link]()*10 + 1) generates numbers from:


a) 0–10 b) 1–10 c) 1–9 d) 2–11

19. [Link]([Link](2, 8), 5) evaluates to:


a) 8 b) 5 c) 2 d) 3
20. [Link]([Link](-16)) returns:
a) -4 b) 4 c) 8 d) 16

21. [Link](7.8) returns a value of type:


a) double b) float c) int d) long

22. Which statement generates a random integer from 0 to 4?


a) [Link]()*4
b) (int)([Link]()*4)
c) (int)[Link]()*4
d) [Link](4)

23. [Link](-3.2) results in:


a) -4 b) -3 c) 3 d) 4

24. Which is a valid Math method call?


a) [Link](2)
b) pow(2,3)
c) [Link](2,3)
d) Math(2,3,pow)

25. [Link]([Link](-4.6)) equals:


a) 5.0 b) 4.0 c) 4.6 d) -5.0

SECTION B – Short Answer – 2 Marks Each (15 Questions)

A. Find the Output (8 questions)

1. [Link]([Link](-4.1));
2. [Link]([Link](6.5));
3. [Link]([Link]([Link](-8, 5)));
4. [Link]([Link]([Link](50)));
5. [Link]([Link](2, [Link](3, 5)));
6. [Link]([Link](7.99) + [Link](2.01));
7. [Link]([Link](64) + [Link](49));
8. [Link]((int)([Link]()*4));
B. Fix the Error

9. Correct the statement:


double x = [Link](49);
10. Correct the statement:
int v = [Link](6.89);
11. Correct the statement:
[Link]([Link](5));
12. Correct the statement:
double r = Random();
13. Correct the statement:
int x = [Link](2, 3);

14. State the minimum and maximum value that can be generated by the
following expression:

(int)([Link]()*20 + 5);

15. Evaluate the following expression and show steps:

[Link](25, [Link](10, 20)) + [Link](-12)

16. A student writes the following code to generate a number from 1 to 10:

int n = (int)[Link]()*10 + 1;

(a) Explain the error.


(b) Write the correct statement.

17. Find the output:

[Link]([Link](5.1) + [Link](5.9) - [Link](5.5));

18. Rewrite the following expression using only Math functions (no operators):

int result = (a > b) ? a : b;

(Hint: Use a Math method.)


19. Evaluate:

[Link]([Link](-36)) + [Link](27)

20. Give the range of values generated by:

(int)([Link]()*50);

Does the range include 50? Justify.

21. Identify and correct the error:

double r = [Link](4.7);

[Link] the output:

[Link]([Link]([Link](2, 8), [Link](8.6)));

Common questions

Powered by AI

In the expression (int)(Math.random()*20 + 5), mathematical operations follow typical precedence rules. The method Math.random() executes first as it does not depend on precedence. The multiplication by 20, due to its higher precedence than addition, occurs next. Lastly, the result is added to 5. The enclosing cast operation to integer has lower precedence and truncates the sum's final result. Therefore, arithmetic operations correctly follow multiplication over addition, modulated by parentheses necessary for casting.

(int)(Math.random() * 6) works by first multiplying a random float greater than or equal to 0.0 and less than 1.0 by 6, which scales it from 0.0 up to (but not including) 6. The integer cast truncates any decimal, resulting in integers from 0 to 5. No integer 6 will ever be reached because Math.random() cannot return a full 1.0.

Math.pow(2, Math.max(3, 5)) first evaluates Math.max(3, 5), which returns 5 as the maximum value. Then, Math.pow(2, 5) calculates 2 raised to the power of 5, resulting in 32.

Math.min(-8, 5) evaluates to -8 because it returns the lesser value of the two arguments. Applying Math.abs to -8 results in 8, as Math.abs returns the absolute value, or non-negative distance from zero, of its argument. Therefore, Math.abs(Math.min(-8, 5)) evaluates to 8.

Math.max(2, 8) evaluates to 8 as it returns the higher of the two numbers. Math.round(8.6) results in 9 because it rounds the float to the nearest integer. Math.min then compares these two values, 8 and 9, and returns the smaller one, which is 8. Thus, Math.min(Math.max(2, 8), Math.round(8.6)) evaluates to 8.

The code snippet double r = Random(); is incorrect because Random() is not a method call but possibly an intended class type. To generate a random number in Java using the Math class, the correct usage would be double r = Math.random(); which uses the static method random from the Math class to produce a double value greater than or equal to 0.0 and less than 1.0.

Math.floor(7.99) returns 7 because floor finds the largest integer less than or equal to 7.99. Math.ceil(2.01) returns 3 because ceiling finds the smallest integer greater than or equal to 2.01. Thus, the expression Math.floor(7.99) + Math.ceil(2.01) evaluates to 7 + 3, which is 10.

The expression int n = (int)Math.random()*10 + 1 is incorrect because it first performs the cast to integer on the result of Math.random(), which always results in 0, as Math.random() returns values between 0.0 and 1.0, so the multiplication and addition become irrelevant. The corrected expression should be int n = (int)(Math.random()*10) + 1; this ensures the randomness affects a range after applying Math.random() to 10, converting to an integer before adding 1.

The expression (int)(Math.random()*10+1) works because Math.random() generates a double greater than or equal to 0.0 and less than 1.0. When multiplied by 10, it scales this to a range from 0.0 to 10.0, but not including 10.0. Adding 1 shifts the range to 1.0 to 11.0. Casting to an integer truncates the decimal, giving integers from 1 to 10. Without proper placement of the int cast, for example, if written as (int)Math.random()*10+1, it would cast the random decimal to an integer immediately (resulting in 0), and then multiply by 10, always producing 1 as the result.

Math.cbrt is a specific method for calculating the cube root of a number, providing a straightforward API call for this purpose. It directly computes the cube root of a given number. Math.pow can also compute cube roots, but more indirectly by using the exponent 1/3, i.e., Math.pow(64, 1.0/3.0). Math.cbrt(64) returns 4 because the cube root of 64 is 4.

You might also like