This set of Tough Java Questions and Answers on “Data Type-BigDecimal”.
1. Which of the following is the advantage of BigDecimal over double? Answer: d 2. Which of the below data type doesn’t support overloaded methods for +,-,* and /? Answer: d 3. What will be the output of the following Java code snippet? a) b) c) d) Answer: a 4. What is the base of BigDecimal data type? Answer: c 5. What is the limitation of toString() method of BigDecimal? Answer: d 6. Which of the following is not provided by BigDecimal? Answer: b 7. BigDecimal is a part of which package? Answer: b 8. What is BigDecimal.ONE? Answer: d 9. Which class is a library of functions to perform arithmetic operations of BigInteger and BigDecimal? Answer: a 10. What will be the output of the following Java code snippet? a) Compilation failure c) d) Runtime exception Answer: b To practice tough questions and answers on all areas of Java, here is complete set on Multiple Choice Questions and Answers on Java.
a) Syntax
b) Memory usage
c) Garbage creation
d) Precision
Clarification: BigDecimal has unnatural syntax, needs more memory and creates a great amount of garbage. But it has a high precision which is useful for some calculations like money.
a) int
b) float
c) double
d) BigDecimal
Clarification: int, float, double provide overloaded methods for +,-,* and /. BigDecimal does not provide these overloaded methods.
double a = 0.02;
double b = 0.03;
double c = b - a;
System.out.println(c);
BigDecimal _a = new BigDecimal("0.02");
BigDecimal _b = new BigDecimal("0.03");
BigDecimal _c = b.subtract(_a);
System.out.println(_c);
0.009999999999999998
0.01
0.01
0.009999999999999998
0.01
0.01
0.009999999999999998
0.009999999999999998
Clarification: BigDecimal provides more precision as compared to double. Double is faster in terms of performance as compared to BigDecimal.
a) Base 2
b) Base 8
c) Base 10
d) Base e
Clarification: A BigDecimal is n*10^scale where n is an arbitrary large signed integer. Scale can be thought of as the number of digits to move the decimal point to left or right.
a) There is no limitation
b) toString returns null
c) toString returns the number in expanded form
d) toString uses scientific notation
Clarification: toString() of BigDecimal uses scientific notation to represent numbers known as canonical representation. We must use toPlainString() to avoid scientific notation.
a) scale manipulation
b) + operator
c) rounding
d) hashing
Clarification: toBigInteger() converts BigDecimal to a BigInteger.toBigIntegerExact() converts this BigDecimal to a BigInteger by checking for lost information.
a) java.lang
b) java.math
c) java.util
d) java.io
Clarification: BigDecimal is a part of java.math. This package provides various classes for storing numbers and mathematical operations.
a) wrong statement
b) custom defined statement
c) static variable with value 1 on scale 10
d) static variable with value 1 on scale 0
Clarification: BigDecimal.ONE is a static variable of BigDecimal class with value 1 on scale 0.
a) MathContext
b) MathLib
c) BigLib
d) BigContext
Clarification: MathContext class is a library of functions to perform arithmetic operations of BigInteger and BigDecimal.
public class AddDemo
{
public static void main(String args[])
{
BigDecimal b = new BigDecimal("23.43");
BigDecimal br = new BigDecimal("24");
BigDecimal bres = b.add(new BigDecimal("450.23"));
System.out.println("Add: "+bres);
MathContext mc = new MathContext(2, RoundingMode.DOWN);
BigDecimal bdecMath = b.add(new BigDecimal("450.23"), mc);
System.out.println("Add using MathContext: "+bdecMath);
}
}
b) Add: 473.66
Add using MathContext: 4.7E+2
Add 4.7E+2
Add using MathContext: 473.66
Clarification: add() adds the two numbers, MathContext provides library for carrying out various arithmetic operations.