Java Questions & Answers – Java.lang – Double & Float Wrappers

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Double & Float Wrappers”.

1. Which of these is a super class of wrappers Double and Float?
a) Long
b) Digits
c) Float
d) Number
View Answer

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()
View Answer

Answer: a
Explanation: None.

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()
View Answer

Answer: a
Explanation: isNaN() methods returns true if num specified is not a number, otherwise it returns false.
advertisement
advertisement

4. Which of these method of Double wrapper can be used to check whether a given value is infinite or not?
a) Infinite()
b) isInfinite()
c) checkInfinite()
d) None of the mentioned
View Answer

Answer: b
Explanation: isInfinite() methods returns true if the specified value is an infinite value otherwise it returns false.

5. Which of these exceptions is thrown by compareTo() method defined in a double wrapper?
a) IOException
b) SystemException
c) CastException
d) ClassCastException
View Answer

Answer: d
Explanation: compareTo() methods compare the specified object to be double, if it is not then ClassCastException is thrown.

6. What will be the output of the following Java code?

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Double i = new Double(257.5);  
  6.             boolean x = i.isNaN();
  7.             System.out.print(x);
  8.         }
  9.     }

a) true
b) false
c) 0
d) 1
View Answer

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.
Output:

advertisement
$ javac Output.java
$ java Output
false

7. What will be the output of the following Java code?

advertisement
  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Double i = new Double(257.578);  
  6.             int x = i.intValue();
  7.             System.out.print(x);
  8.         }
  9.     }

a) 0
b) 1
c) 256
d) 257
View Answer

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.
Output:

$ javac Output.java
$ java Output
257

8. What will be the output of the following Java code?

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5. 	    Double i = new Double(257.578123456789);  
  6.             float x = i.floatValue();
  7.             System.out.print(x);
  8.         }
  9.     }

a) 0
b) 257.0
c) 257.57812
d) 257.578123456789
View Answer

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.
Output:

$ javac Output.java
$ java Output
257.57812

9. What will be the output of the following Java code?

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Double y = new Double(257.57812);
  6. 	    Double i = new Double(257.578123456789);  
  7.             try
  8.             {
  9. 	        int x = i.compareTo(y);
  10.                 System.out.print(x);
  11.             }
  12.             catch(ClassCastException e)
  13.             {
  14.                 System.out.print("Exception");
  15.             }
  16. 	}
  17.     }

a) 0
b) 1
c) Exception
d) None of the mentioned
View Answer

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.
Output:

$ javac Output.java
$ java Output
1

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.