Java Questions & Answers – Java.lang – Byte & Short Wrappers

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Java.lang – Byte & Short Wrappers”.

1. Which of these methods of Byte wrapper can be used to obtain Byte object from a string?
a) toString()
b) getString()
c) decode()
d) encode()
View Answer

Answer: c
Explanation: decode() methods returns a Byte object that contains the value specified by string.

2. Which of the following methods Byte wrapper return the value as a double?
a) doubleValue()
b) converDouble()
c) getDouble()
d) getDoubleValue()
View Answer

Answer: a
Explanation: doubleValue() returns the value of invoking object as double.

3. Which of these is a super class of wrappers Byte and short wrappers?
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.
advertisement
advertisement

4. Which of these methods is not defined in both Byte and Short wrappers?
a) intValue()
b) isInfinite()
c) toString()
d) hashCode()
View Answer

Answer: b
Explanation: isInfinite() methods is defined in Integer and Long Wrappers, returns true if specified value is an infinite value otherwise it returns false.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Double i = new Double(257.5);  
  6.             Double x = i.MAX_VALUE;
  7.             System.out.print(x);
  8.         }
  9.     }

a) 0
b) 1.7976931348623157E308
c) 1.7976931348623157E30
d) None of the mentioned
View Answer

Answer: b
Explanation: The Double class defines the constant MAX_VALUE above which a number is considered to be infinity. MAX_VALUE is 1.7976931348623157E308.

advertisement

In the given code, we are using an instance variable i to get the MAX_VALUE, instead of the using the class name itself i.e., Double.MAX_VALUE. So, while the code will compile and run, it’s generally considered better practice to access static constants in Java through the class name rather than through an instance of the class.

Output:

advertisement
$ javac Output.java
$ java Output
1.7976931348623157E308

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

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

a) 0
b) 4.9E-324
c) 1.7976931348623157E308
d) None of the mentioned
View Answer

Answer: b
Explanation: The Double class defines the constant MIN_VALUE which represents the smallest positive nonzero value that can be represented by a double primitive type. MIN_VALUE is 4.9E-324.

In the given code, we are using an instance variable i to get the MIN_VALUE, instead of the using the class name itself i.e., Double.MIN_VALUE. So, while the code will compile and run, it’s generally considered better practice to access static constants in Java through the class name rather than through an instance of the class.
Output:

$ javac Output.java
$ java Output
4.9E-324

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

  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

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.