250+ TOP MCQs on Data Type-BigDecimal and Answers

This set of Tough Java Questions and Answers on “Data Type-BigDecimal”.

1. Which of the following is the advantage of BigDecimal over double?
a) Syntax
b) Memory usage
c) Garbage creation
d) Precision

Answer: d
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.

2. Which of the below data type doesn’t support overloaded methods for +,-,* and /?
a) int
b) float
c) double
d) BigDecimal

Answer: d
Clarification: int, float, double provide overloaded methods for +,-,* and /. BigDecimal does not provide these overloaded methods.

3. What will be the output of the following Java code snippet?

  1.    double a = 0.02;
  2.    double b = 0.03;
  3.    double c = b - a;
  4.    System.out.println(c);
  5.  
  6.    BigDecimal _a = new BigDecimal("0.02");
  7.    BigDecimal _b = new BigDecimal("0.03");
  8.    BigDecimal _c = b.subtract(_a);
  9.    System.out.println(_c);

a)

   0.009999999999999998
   0.01

b)

   0.01
   0.009999999999999998

c)

   0.01
   0.01

d)

   0.009999999999999998
   0.009999999999999998

Answer: a
Clarification: 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

Answer: c
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.

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

Answer: d
Clarification: 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
c) rounding
d) hashing

Answer: b
Clarification: 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?
a) java.lang
b) java.math
c) java.util
d) java.io

Answer: b
Clarification: 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

Answer: d
Clarification: 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?
a) MathContext
b) MathLib
c) BigLib
d) BigContext

Answer: a
Clarification: MathContext class is a library of functions to perform arithmetic operations of BigInteger and BigDecimal.

10. What will be the output of the following Java code snippet?

  1. public class AddDemo 
  2. {
  3. 	public static void main(String args[]) 
  4.         {
  5. 		BigDecimal b = new BigDecimal("23.43");
  6. 		BigDecimal br = new BigDecimal("24");
  7. 		BigDecimal bres = b.add(new BigDecimal("450.23"));
  8. 		System.out.println("Add: "+bres);
  9.  
  10. 		MathContext mc = new MathContext(2, RoundingMode.DOWN);
  11. 		BigDecimal bdecMath = b.add(new BigDecimal("450.23"), mc);
  12. 		System.out.println("Add using MathContext: "+bdecMath);
  13. 	}
  14. }

a) Compilation failure
b)

Add: 473.66
Add using MathContext: 4.7E+2

c)

Add 4.7E+2
Add using MathContext: 473.66

d) Runtime exception

Answer: b
Clarification: add() adds the two numbers, MathContext provides library for carrying out various arithmetic operations.

To practice tough questions and answers on all areas of Java, here is complete set on Multiple Choice Questions and Answers on Java.

contest

Leave a Reply

Your email address will not be published. Required fields are marked *