This set of Tough Java Questions and Answers focuses on “Data Type-BigDecimal”.
1. Which of the following is the advantage of BigDecimal over double?
b) Memory usage
c) Garbage creation
Explanation: 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.
2. Which of the below data type doesn’t support overloaded methods for +,-,* and /?
Explanation: int, float, double provide overloaded methods for +,-,* and /. BigDecimal does not provide these overloaded methods.
3. What is the output of below code snippet?
double a = 0.02;
double b = 0.03;
double c = b - a;
BigDecimal _a = new BigDecimal("0.02");
BigDecimal _b = new BigDecimal("0.03");
BigDecimal _c = b.subtract(_a);
0.009999999999999998 0.009999999999999998View Answer
Explanation: BigDecimal provides more precision as compared to double. Double is faster in terms of performance as compared to BigDecimal.
4. What is the base of BigDecimal data type?
a) Base 2
b) Base 8
c) Base 10
d) Base e
Explanation: 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.
5. What is the limitation of toString() method of BigDecimal?
a) There is no limitation
b) toString returns null
c) toString returns the number in expanded form
d) toString uses scientific notation
Explanation: toString() of BigDecimal uses scientific notation to represent numbers known as canonical representation. We must use toPlainString() to avoid scientific notation.
6. Which of the following is not provided by BigDecimal?
a) scale manipulation
b) + operator
Explanation: toBigInteger() converts BigDecimal to a BigInteger.toBigIntegerExact() converts this BigDecimal to a BigInteger by checking for lost information.
7. BigDecimal is a part of which package?
Explanation: BigDecimal is a part of java.math. This package provides various classes for storing numbers and mathematical operations.
8. What is BigDecimal.ONE?
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
Explanation: BigDecimal.ONE is a static variable of BigDecimal class with value 1 on scale 0.
9. Which class is a library of functions to perform arithmetic operations of BigInteger and BigDecimal?
Explanation: MathContext class is a library of functions to perform arithmetic operations of BigInteger and BigDecimal.
10. What is the output of below code snippet?
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"));
MathContext mc = new MathContext(2, RoundingMode.DOWN);
BigDecimal bdecMath = b.add(new BigDecimal("450.23"), mc);
System.out.println("Add using MathContext: "+bdecMath);
a) Compilation failure
Add: 684.66 Add using MathContext: 6.8E+2
Add 6.8E+2 Add using MathContext: 684.66
d) Runtime exception
Explanation: add() adds the two numbers, MathContext provides library for carrying out various arithmetic operations.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice tough questions and answers on all areas of Java, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.