Q1. Which package does the Math class belong to?
A1. java.lang
Q2. What is the fully qualified name of the Math class in Java?
A2. java.lang.Math
Q3. Name the two static variables of the Math class. Write a program that prints out their values.
A3. The two static variables of the Math class are “E” and “PI”. The program and the output are shown below:
class Q3
{
public static void main(String[] args)
{
System.out.println( "Math.E: " + Math.E );
System.out.println( "Math.PI: " + Math.PI );
}
}
Output:
Math.E-: 2.71828
Math.PI-: 3.14159
Q4. Give the name of the Math class function that performs the following:
a. Returns the absolute value of the argument passed
b. Returns the largest non-fractional value less than or equal to the argument passed
c. Returns the smallest non-fractional value greater than or equal to the argument passed
d. Returns the greater of two arguments
e. Returns the lower of two arguments
f. Returns the natural log of the passed argument
g. Returns the log to base 10 of the passed argument
h. Returns the power of argument 1 raised to argument 2
i. Returns the closest non-fractional number to the passed argument
j. Returns a random number >= 0.0 and < 1
k. Returns the square root of the passed argument
l. Returns a “double” value that is closest in value to the argument passed, and is non-fractional.
A4.
a. Math.abs()
b. Math.floor()
c. Math.ceil()
d. Math.max()
e. Math.min()
f. Math.log()
g. Math.log10()
h. Math.pow()
i. Math.round()
j. Math.random()
k. Math.sqrt()
l. Math.rint()
Q5. Will the following code snippet compile? If no, why not? If yes, what is the output?
double d = Math.E;
int D = Math.round(d);
System.out.println("d: " + d);
System.out.println("D: " + D);
A5. The code snippet will NOT compile. The Math.round() method returns a “long” when the argument passed is “double”, and returns an “int” when the argument passed is “float”. In this case, the argument passed is a “double”, and hence the return value is “long”, which we are trying to store in integer variable “D”. This implicit typecast from “long” to “int” is not allowed, and hence this code will not compile. The compiler error will be “possible loss of precision”.
Q6. Will the following code snippet compile? If no, why not? If yes, what is the output?
double d1 = 2.3, d2 = 4.5, d;
int i1 = 4, i2 = 2, i;
d = Math.max( d1, d2 );
i = Math.max( i1, i2 );
System.out.println("d: " + d);
System.out.println("i: " + i);
A6. Yes, the code will compile. This is because the Math.max() and Math.min() functions are overloaded and specially constructed such that the return type is the greatest data type of the two arguments passed. In the first case, the data type returned is “double”, and the in the second case it is “int”. Both will compile. The output is:
d: 4.5
i: 4
Q7. Will the following code snippet compile? If no, why not? If yes, what is the output?
int i = Math.min( 3L, 2 );
System.out.println( "i: " + i );
A7. The code snippet will NOT compile. The arguments passed to Math.min() are “long” and “int”. Hence the returned data type will be the largest of the two, which is “long”. The LHS is of type “int”. Since “long” cannot be implicitly converted into “int”, the code snippet will not compile. The compiler error would be “possible loss of precision”.
Q8. What is the largest value that can come out of the expression:
(int)(Math.random() * 100) ?
(int)Math.round( Math.random() * 100 ) ?
A8. In the first case, the largest value is 99, since Math.random() returns a number less than 1.0, hence the maximum value can be 99.99999… which, when typecast to (int), will mean a maximum value of 99 only.
In the second case, we have a Math.round() around the 99.9999… which means that a maximum value of 100 is also possible.
Q9. Will the following code snippet compile? If no, why not? If yes, what is the answer?
int area = 16;
int side = Math.sqrt( area );
System.out.println("Side of square: " + side);
A9. The code snippet will NOT compile. This is because the Math.sqrt() method returns a double argument, which cannot be implicitly typecast into an int datatype.
Q10. Will the following code snippet compile? If no, why not? If yes, what is the output?
int area = 16;
int side = (int)Math.sqrt( area );
System.out.println("Side of square: " + side);
A10. The code snippet will compile. The output is:
Side of square : 4
Q11. Explain the difference between the Math.round() and Math.rint() functions of the Math.class in Java?
A11. The Math.round() method will return either a “long” or an “int” value of the argument passed. The Math.rint() method will return a “double” value of the argument passed. In the case of Math.round(), there are two signatures: the one which accepts a “double” argument, will return a “long”, and the one which accepts a “float”, “long” or “int” argument, will return an “int”. The Math.rint() always returns a “double” value.
Q12. Will the following code snippet compile? If no, why not? If yes, what is the output?
int i = Math.max( 4L, 3F );
long l = Math.max( 4L, 3F );
float f = Math.max( 4L, 3F );
double d = Math.max( 4L, 3F );
A12. The code snippet will NOT compile. There are two errors for the same reason. In all 4 statements, the arguments passed to Math.max are long and float. The greater of these is float. Hence, the return type should be float or double, nothing else. In the first two statements, the return type is int and long, which cannot be used for implicit type from float. These two statements will result in a “possible loss of precision” error, and the code will not compile.
Q13. Rewrite the code after removing the unnecessary typecasts from the code snippet below.
byte b1 = 2, b2 = 3;
short s1, s2;
int i1, i2;
s1 = (byte)(b1+b2);
s2 = (short)(b1+b2);
i1 = (int)b1 + (int)s2;
i2 = (int)(i1 + b1 + b2);
A13.
byte b1 = 2, b2 = 3;
short s1, s2;
int i1, i2;
s1 = (byte)(b1+b2);
s2 = (short)(b1+b2);
i1 = b1 + s2;
i2 = i1 + b1 + b2;
The typecasts in the last two lines are removed. They are not needed since the RHS is of type int, which is the same as the datatype of the LHS variable.
Q14. What is the output of the following code snippet?
System.out.println( Math.log( Math.E ) );
System.out.println( Math.log10( 10 ) );
A14. The first statement is the natural log (to base E) of E itself, which is 1.0. The second statement is log of 10 to base 10, which is also 1.0. The return value is of type double. The output will be:
1.0
1.0
Q15. What is the output of the following code snippet?
int x = 4;
double y = 9.0;
double z = 6.25;
System.out.println( "x: " + Math.sqrt(x) );
System.out.println( "y: " + Math.sqrt(y) );
System.out.println( "z: " + Math.sqrt(z) );
A15.
x: 2.0
y: 3.0
z: 2.5
Explanation: The return data type of Math.sqrt() is always “double”, and hence the decimal point will be printed.
Q16. Will the following code compile? If no, why not? If yes, what is the output?
byte b = (byte)Math.sqrt(9);
b *= 2;
System.out.println("b: " + b);
A16. Yes, the code snippet will compile. The output is:
b: 6
Explanation: The statement b *= 2; is a little tricky. Normally, b = b * 2; will not compile since the RHS is an int, which cannot be assigned implicitly to the LHS (which is byte). But in shortcut operators, this is automatically converted.
Q17. Will the following code snippet compile? If no, why not? How many errors do you see? If yes, what is the output?
byte b = (byte)Math.sqrt(9);
byte c = (byte)(Math.random() * 1);
b = b * 2;
c += 100;
System.out.println("b: " + b);
System.out.println("c: " + c);
A17. The code snippet will not compile. There is one error in the code snippet above. That error is with the statement “b = b * 2;”. The RHS is of type “int”, whereas the LHS is of type “byte”. An explicit typecast is needed for this conversion, which is missing. In the statement “c += 100″, no explicit typecast is needed, since with shortcut operators, this type conversion is implicit.
Q18. Why are the functions of the Math class static?
A18. The Math class is a utility class that provides commonly needed functions. There is no need to have its functions non-static. Had the functions been non-static, we would first have to create an object of the Math class, and then use that object when calling the functions – nothing gained by doing so. Rather, it is best to use Math.sqrt() for example, as a static function.
A1. java.lang
Q2. What is the fully qualified name of the Math class in Java?
A2. java.lang.Math
Q3. Name the two static variables of the Math class. Write a program that prints out their values.
A3. The two static variables of the Math class are “E” and “PI”. The program and the output are shown below:
class Q3
{
public static void main(String[] args)
{
System.out.println( "Math.E: " + Math.E );
System.out.println( "Math.PI: " + Math.PI );
}
}
Output:
Math.E-: 2.71828
Math.PI-: 3.14159
Q4. Give the name of the Math class function that performs the following:
a. Returns the absolute value of the argument passed
b. Returns the largest non-fractional value less than or equal to the argument passed
c. Returns the smallest non-fractional value greater than or equal to the argument passed
d. Returns the greater of two arguments
e. Returns the lower of two arguments
f. Returns the natural log of the passed argument
g. Returns the log to base 10 of the passed argument
h. Returns the power of argument 1 raised to argument 2
i. Returns the closest non-fractional number to the passed argument
j. Returns a random number >= 0.0 and < 1
k. Returns the square root of the passed argument
l. Returns a “double” value that is closest in value to the argument passed, and is non-fractional.
A4.
a. Math.abs()
b. Math.floor()
c. Math.ceil()
d. Math.max()
e. Math.min()
f. Math.log()
g. Math.log10()
h. Math.pow()
i. Math.round()
j. Math.random()
k. Math.sqrt()
l. Math.rint()
Q5. Will the following code snippet compile? If no, why not? If yes, what is the output?
double d = Math.E;
int D = Math.round(d);
System.out.println("d: " + d);
System.out.println("D: " + D);
A5. The code snippet will NOT compile. The Math.round() method returns a “long” when the argument passed is “double”, and returns an “int” when the argument passed is “float”. In this case, the argument passed is a “double”, and hence the return value is “long”, which we are trying to store in integer variable “D”. This implicit typecast from “long” to “int” is not allowed, and hence this code will not compile. The compiler error will be “possible loss of precision”.
Q6. Will the following code snippet compile? If no, why not? If yes, what is the output?
double d1 = 2.3, d2 = 4.5, d;
int i1 = 4, i2 = 2, i;
d = Math.max( d1, d2 );
i = Math.max( i1, i2 );
System.out.println("d: " + d);
System.out.println("i: " + i);
A6. Yes, the code will compile. This is because the Math.max() and Math.min() functions are overloaded and specially constructed such that the return type is the greatest data type of the two arguments passed. In the first case, the data type returned is “double”, and the in the second case it is “int”. Both will compile. The output is:
d: 4.5
i: 4
Q7. Will the following code snippet compile? If no, why not? If yes, what is the output?
int i = Math.min( 3L, 2 );
System.out.println( "i: " + i );
A7. The code snippet will NOT compile. The arguments passed to Math.min() are “long” and “int”. Hence the returned data type will be the largest of the two, which is “long”. The LHS is of type “int”. Since “long” cannot be implicitly converted into “int”, the code snippet will not compile. The compiler error would be “possible loss of precision”.
Q8. What is the largest value that can come out of the expression:
(int)(Math.random() * 100) ?
(int)Math.round( Math.random() * 100 ) ?
A8. In the first case, the largest value is 99, since Math.random() returns a number less than 1.0, hence the maximum value can be 99.99999… which, when typecast to (int), will mean a maximum value of 99 only.
In the second case, we have a Math.round() around the 99.9999… which means that a maximum value of 100 is also possible.
Q9. Will the following code snippet compile? If no, why not? If yes, what is the answer?
int area = 16;
int side = Math.sqrt( area );
System.out.println("Side of square: " + side);
A9. The code snippet will NOT compile. This is because the Math.sqrt() method returns a double argument, which cannot be implicitly typecast into an int datatype.
Q10. Will the following code snippet compile? If no, why not? If yes, what is the output?
int area = 16;
int side = (int)Math.sqrt( area );
System.out.println("Side of square: " + side);
A10. The code snippet will compile. The output is:
Side of square : 4
Q11. Explain the difference between the Math.round() and Math.rint() functions of the Math.class in Java?
A11. The Math.round() method will return either a “long” or an “int” value of the argument passed. The Math.rint() method will return a “double” value of the argument passed. In the case of Math.round(), there are two signatures: the one which accepts a “double” argument, will return a “long”, and the one which accepts a “float”, “long” or “int” argument, will return an “int”. The Math.rint() always returns a “double” value.
Q12. Will the following code snippet compile? If no, why not? If yes, what is the output?
int i = Math.max( 4L, 3F );
long l = Math.max( 4L, 3F );
float f = Math.max( 4L, 3F );
double d = Math.max( 4L, 3F );
A12. The code snippet will NOT compile. There are two errors for the same reason. In all 4 statements, the arguments passed to Math.max are long and float. The greater of these is float. Hence, the return type should be float or double, nothing else. In the first two statements, the return type is int and long, which cannot be used for implicit type from float. These two statements will result in a “possible loss of precision” error, and the code will not compile.
Q13. Rewrite the code after removing the unnecessary typecasts from the code snippet below.
byte b1 = 2, b2 = 3;
short s1, s2;
int i1, i2;
s1 = (byte)(b1+b2);
s2 = (short)(b1+b2);
i1 = (int)b1 + (int)s2;
i2 = (int)(i1 + b1 + b2);
A13.
byte b1 = 2, b2 = 3;
short s1, s2;
int i1, i2;
s1 = (byte)(b1+b2);
s2 = (short)(b1+b2);
i1 = b1 + s2;
i2 = i1 + b1 + b2;
The typecasts in the last two lines are removed. They are not needed since the RHS is of type int, which is the same as the datatype of the LHS variable.
Q14. What is the output of the following code snippet?
System.out.println( Math.log( Math.E ) );
System.out.println( Math.log10( 10 ) );
A14. The first statement is the natural log (to base E) of E itself, which is 1.0. The second statement is log of 10 to base 10, which is also 1.0. The return value is of type double. The output will be:
1.0
1.0
Q15. What is the output of the following code snippet?
int x = 4;
double y = 9.0;
double z = 6.25;
System.out.println( "x: " + Math.sqrt(x) );
System.out.println( "y: " + Math.sqrt(y) );
System.out.println( "z: " + Math.sqrt(z) );
A15.
x: 2.0
y: 3.0
z: 2.5
Explanation: The return data type of Math.sqrt() is always “double”, and hence the decimal point will be printed.
Q16. Will the following code compile? If no, why not? If yes, what is the output?
byte b = (byte)Math.sqrt(9);
b *= 2;
System.out.println("b: " + b);
A16. Yes, the code snippet will compile. The output is:
b: 6
Explanation: The statement b *= 2; is a little tricky. Normally, b = b * 2; will not compile since the RHS is an int, which cannot be assigned implicitly to the LHS (which is byte). But in shortcut operators, this is automatically converted.
Q17. Will the following code snippet compile? If no, why not? How many errors do you see? If yes, what is the output?
byte b = (byte)Math.sqrt(9);
byte c = (byte)(Math.random() * 1);
b = b * 2;
c += 100;
System.out.println("b: " + b);
System.out.println("c: " + c);
A17. The code snippet will not compile. There is one error in the code snippet above. That error is with the statement “b = b * 2;”. The RHS is of type “int”, whereas the LHS is of type “byte”. An explicit typecast is needed for this conversion, which is missing. In the statement “c += 100″, no explicit typecast is needed, since with shortcut operators, this type conversion is implicit.
Q18. Why are the functions of the Math class static?
A18. The Math class is a utility class that provides commonly needed functions. There is no need to have its functions non-static. Had the functions been non-static, we would first have to create an object of the Math class, and then use that object when calling the functions – nothing gained by doing so. Rather, it is best to use Math.sqrt() for example, as a static function.