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

This section of our 1000+ Java MCQs focuses on Double and Float wrappers of Java Programming Language.

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.

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. Which of these exceptions is thrown by compareTo() method defined in 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 is the output of this program?

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

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

Answer: b
Explanation: The super class of Double class defines a constant MAX_VALUE above which a number is considered to be infinity. MAX_VALUE is 1.7976931348623157E308.
Output:
$ javac Output.java
$ java Output
1.7976931348623157E308

7. What is the output of this program?

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

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

Answer: b
Explanation: The super class of Byte class defines a constant MIN_VALUE below which a number is considered to be negative infinity. MIN_VALUE is 4.9E-324.
Output:
$ javac Output.java
$ java Output
4.9E-324

8. What is the output of this program?

  1.     class Output {
  2.          public static void main(String args[]) {
  3.              Integer i = new Integer(257);  
  4.              byte x = i.byteValue();
  5.              System.out.print(x);
  6.         }
  7.     }

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

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.
Output:
$ javac Output.java
$ java Output
1

9. What is the output of this program?

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

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

10. What is the output of this program?

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

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.

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Follow Manish & Sanfoundry