32). Which of the following is the advantage of BigDecimal over double?
[A]Syntax
[B]Memory usage
[C]Garbage creation
[D]Precision
Show Answer
Correct Answer: Precision
Notes:
Answer: d
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.
33). Which of the below data type doesn’t support overloaded methods for +,-,* and /?
[A]int
[B]float
[C]double
[D]BigDecimal
Show Answer
Correct Answer: BigDecimal
Notes:
Answer: d
Explanation: int, float, double provide overloaded methods for +,-,* and /. BigDecimal does not provide these overloaded methods.
34). What will be the output of the following Java code snippet?
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);
[A] 0.009999999999999998 0.01
[B] 0.01 0.009999999999999998
[C] 0.01 0.01
[D] 0.009999999999999998 0.009999999999999998
Show Answer
Correct Answer: 0.009999999999999998 0.01
Notes:
Answer: a
Explanation: BigDecimal provides more precision as compared to double. Double is faster in terms of performance as compared to BigDecimal.
35). What is the base of BigDecimal data type?
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);
[A]Base 2
[B]Base 8
[C]Base 10
[D]Base e
Show Answer
Correct Answer: Base 10
Notes:
Answer: c
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.
36). What is the limitation of toString() method of BigDecimal?
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);
[A]There is no limitation
[B]toString returns null
[C]toString returns the number in expanded form
[D]toString uses scientific notation
Show Answer
Correct Answer: toString uses scientific notation
Notes:
Answer: d
Explanation: toString() of BigDecimal uses scientific notation to represent numbers known as canonical representation. We must use toPlainString() to avoid scientific notation.
37). Which of the following is not provided by BigDecimal?
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);
[A]scale manipulation
[B] + operator
[C]rounding
[D]hashing
Show Answer
Correct Answer: + operator
Notes:
Answer: b
Explanation: toBigInteger() converts BigDecimal to a BigInteger.toBigIntegerExact() converts this BigDecimal to a BigInteger by checking for lost information.
38). BigDecimal is a part of which package?
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);
[A]java.lang
[B]java.math
[C]java.util
[D]java.io
Show Answer
Correct Answer: java.math
Notes:
Answer: b
Explanation: BigDecimal is a part of java.math. This package provides various classes for storing numbers and mathematical operations.
39). What is BigDecimal.ONE?
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);
[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
Show Answer
Correct Answer: static variable with value 1 on scale 0
Notes:
Answer: d
Explanation: BigDecimal.ONE is a static variable of BigDecimal class with value 1 on scale 0.
40). Which class is a library of functions to perform arithmetic operations of BigInteger and BigDecimal?
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);
[A]MathContext
[B]MathLib
[C]BigLib
[D]BigContext
Show Answer
Correct Answer: MathContext
Notes:
Answer: a
Explanation: MathContext class is a library of functions to perform arithmetic operations of BigInteger and BigDecimal.
41). What will be the output of the following Java 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"));
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);
}
}
[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
Show Answer
Correct Answer: Add: 473.66 Add using MathContext: 4.7E+2
Notes:
Answer: b
Explanation: add() adds the two numbers, MathContext provides library for carrying out various arithmetic operations.