Java Multiple Choice Questions & Answers on Java.lang package Double & Float Wrappers for Freshers

https://www.computersprofessor.com/2018/01/java-multiple-choice-questions-answers_21.html
1. Which of these is a super class of wrappers Double and Float?
a) Long
b) Digits
c) Float
d) Number
Answer: d
Explanation: Number is an abstract class containing subclasses Double, Float, Byte, Short, Integer and Long.
2. Which of the following methods return the value as a double?
a) doubleValue()
b) converDouble()
c) getDouble()
d) getDoubleValue()
Answer: a
3. Which of these methods can be used to check whether the given value is a number or not?
a) isNaN()
b) isNumber()
c) checkNaN()
d) checkNumber()
Answer: a
Explanation: isNaN() methods returns true if num specified is not a number, otherwise it returns false.
4. Which of these method of Double wrapper can be used to check weather a given value is infinite or not?
a) Infinite()
b) isInfinite()
c) checkInfinite()
d) None of the mentioned
Answer: b
Explanation: isInfinite() methods returns true if specified value is an infinite value otherwise it returns false.
5. Which of these exceptions is thrown by compareTo() method defined in double wrapper?
a) IOException
b) SystemException
c) CastException
d) ClassCastException
Answer: d
Explanation: compareTo() methods compare the specified object to be double, if it is not then ClassCastException is thrown.
6. What is the output of this program?
class Output {
public static void main(String args[]) {
Double i = new Double(257.5);
boolean x = i.isNaN();
System.out.print(x);
}
}
a) true
b) false
c) 0
d) 1
b) false
c) 0
d) 1
Answer: b
Explanation: i.isNaN() method returns returns true if i is not a number and false when i is a number. Here false is returned because i is a number i:e 257.5.
7. What is the output of this program?
class Output {
public static void main(String args[]) {
Integer i = new Integer(257);
byte x = i.byteValue();
System.out.print(x);
}
}
a) 0
b) 1
c) 256
d) 257
b) 1
c) 256
d) 257
Answer: b
Explanation: i.byteValue() method returns the value of wrapper i as a byte value. i is 257, range of byte is 256 therefore i value exceeds byte range by 1 hence 1 is returned and stored in x.
8. What is the output of this program?
class Output {
public static void main(String args[]) {
Double i = new Double(257.578);
int x = i.intValue();
System.out.print(x);
}
}
a) 0
b) 1
c) 256
d) 257
b) 1
c) 256
d) 257
Answer: d
Explanation: i.intValue() method returns the value of wrapper i as a Integer. i is 257.578 is double number when converted to an integer data type its value is 257.
9. What is the output of this program?
class Output {
public static void main(String args[]) {
Double i = new Double(257.578123456789);
float x = i.floatValue();
System.out.print(x);
}
}
a) 0
b) 257.0
c) 257.57812
d) 257.578123456789
b) 257.0
c) 257.57812
d) 257.578123456789
Answer: c
Explanation: floatValue() converts the value of wrapper i into float, since float can measure till 5 places after decimal hence 257.57812 is stored in floating point variable x.
10. What is the output of this program?
class Output {
public static void main(String args[]) {
Double y = new Double(257.57812);
Double i = new Double(257.578123456789);
try {
int x = i.compareTo(y);
System.out.print(x);
}
catch(ClassCastException e) {
System.out.print("Exception");
}
}
}
a) 0
b) 1
c) Exception
d) None of the mentioned
b) 1
c) Exception
d) None of the mentioned
Answer: b
Explanation: i.compareTo() methods two double values, if they are equal then 0 is returned and if not equal then 1 is returned, here 257.57812 and 257.578123456789 are not equal hence 1 is returned and stored in x.