Crack Java Math Problems
- Categories Java, Operators, Math Library
Click here -> Java Class Libraries
Lets Start!
- In which package is math class located in Java?
- What is the return type of following Java math functions
- round(3.14)
- pow()
- rint()
- sqrt()
- long
- double
- double
- double
- What is the return type of following Java math functions
- sin()
- ceil()
- round((float)x);
- exp()
- double
- double
- int
- double
- Answer the questions
- What is the range of value returned by random() function
- What is the return type of random() function.
- greater than or equal to 0.0 and less than 1.0 or random no >= 0 and random no < 1.
- double
- Answer the questions
- Write a Java statement to generate a random number from 1 – 10.
- Write a Java statement to generate a random number to simulate a dice.
- (int)(Math.random() * 10) + 1;
- (int)(Math.random() * 6) + 1;
- Give the output of the following:
(i) Math.floor(-4.7) (ii) Math.ceil(3.4) + Math.pow(2, 3)
Ans.i) -5.0 ii) 12.0 Explanation: 4.0 + 8.0
- What are the values stored in variables r1 and r2:
- double r1 = Math.abs(Math.min(-2.83,-5.83));
- double r2 = Math.sqrt(Math.floor(16.3));
- double r1 = Math.abs(Math.min(-2.83,-5.83)); = Math.abs(-5.83) = 5.83
- double r2 = Math.sqrt(Math.floor(16.3)); = Math.sqrt(16) = 4.0
- What will be the output of the following function?
i) Math.pow(9,0.5) ii) Math.min(-21,-12) iii) Math.ceil(5.2) iv) Math.floor(2.9)
Ans i) 3.0 ii) -21 iii) 6.0 iv) 2.0- Give the output of the following Math functions:
(i) Math.ceil(4.2) (ii) Math.abs(-4)
Ans.(i) 5.0 (and not 5, as ceil() returns a double) (ii) 4
- Give the result of the following functions:
- Math.floor(70.3)
- Math.ceil(-10.01)
- Math.sqrt(36)
- Math.abs(-9)
- 70.0
- -10.0
- 6.0
- 9
- What is the value stored in x and y after execution of these statements?
double x = Math.ceil(8.3) + Math.floor(-3.2)+Math.rint(4.5); double y = Math.abs(Math.round(Math.max( -4.4, -9.6)));
Ans.X = 9.0 Working: 9.0 + (-4.0) + 4.0 = 9.0 Y = 4.0 Working Math.abs(Math.round(-4.4)) = 4.0
- What is the output of following statements?
double d1 = -0.5d; System.out.println(“Ceil for dl= ” + Math.ceil( dl ); System.out.println(“Floor for dl= ” +Math.floor (d1 ));
Ans:Ceil for d1= -0.0 Floor for d1= -1.0;
- What are the final values stored in variables x and y below?
double a = – 6.35; double b = 14.74; double x = Math.abs(Math.ceil(a)); double y = Math.rint(Math.max(a,b));
Ans.x=6.0 and y=15.0
- What are the values stored in variables r1 and r2:
(i) double r1 = Math.abs(Math.min(-2.83,-5.83)); (ii) double r2 = Math.sqrt(Math.floor(16.3));
Ans.- (i) r1=5.83 (ii) r2=4.0
- Give the output of the following:
a) Math.floor (-4.7) b) Math.ceil(3.4) + Math.pow(2, 3)
Ans.- a) -5.0 b) 12.0
- Give the results of the following functions:
(i) Math.floor (81.2)
(ii) Math.ceil (-10.01)
(iii) Math.sqrt (25)
(iv) Math.cbrt (-27.0)
Ans:(i)81.0 (ii) -10.0 (iii) 5.0 (iv) -3.0
- Give the output of the following:
i. Math.ceil(4.2) ii. Math.abs(-4)
Ans.i. 5.0 ii. 4
- Give the output of the following:
- Math.max(-17, -19)
- Math.ceil(7.8)
- -17
- 0
- Give the output of the following:
- Math.floor(15.36)
- Math.round(146.5)
- 15.0
- 147
- Give the output of the following:
- Math.max(11,10)
- Math.ceil(-12.56)
- 11
- -12.0
- Give the output of the following:
- Math.sqrt(625)
- Math.cbrt(125)
- 25.0
- 5.0
- Give the output of the following:
- Math.min(-0.0,0.0)
- Math.pow(4,3)
- -0.0
- 64.0
- Give the output of the following:
- Math.floor(-126.349)
- Math.max(45.6,17.3)
- -127.0
- 45.6
- What are the final values stored in variables x and y below?
double a = – 6.35; double b = 14.74; double x = Math.abs(Math.ceil(a)); double y = Math.rint(Math.max(a,b));
Ans.Math.ceil() gives the smallest of all those integers that are greater than the input. So, Math.ceil(-6.35) gives -6.0 (not -6 as ceil returns a double value). Math.abs() gives the absolute value of the number i.e. removes negative sign, if present. So, Math.abs(-6.0) results in 6.0. So, x = 6.0.
Math.max(-6.35, 14.74) returns 14.74. rint() returns the double value which is closest in value to the argument passed and is equal to an integer. So, Math.rint(14.74) returns 15.0. So, y = 15.0.
- Name the type of error (syntax, runtime or logical error) in each case given below:
(i) c = Math.sqrt (36-45);
(ii) int a;b;c;
Ans.(i) Runtime error
Math.sqrt(36-45) = Math.sqrt(-9) which cannot be computed as square root of a negative number is not defined. Therefore, a runtime error will be thrown.
(ii) Syntax error
Multiple variables can be defined in one of the following ways int a, b, c; int a; int b; int c;
- Name the type of error (syntax, runtime or logical error) in each case given below:
(i) a = 5 + (Math.random(10) * 2);
(ii) int a= 5/6-3*2; int b= 15/4+3*2-9; System.out.println(a/b);
Ans.- Syntax error
Math.random() is non-parameterise function. Correct way for Math.random() is as following:
a = 5 + (Math.random() * 2);
- logical error
(a= 15, b=0; a/b=> as any number divided by zero is logical error.)