Library Classes
Library Classes
Question 1
1. Integer
2. Long
3. Byte
4. Double
Answer
Integer
Question 2
1. Float
2. Character
3. String
4. Integer
Answer
Character
Question 3
Which following method of wrapper Integer will convert the value of an object into int?
1. bytevalue( )
2. int intValue( )
3. Bytevalue( )
4. Byte Bytevalue()
Answer
int intValue( )
Reason — int intValue( ) function returns the value of the invoking object as an int.
Question 4
Which of the following is/are not valid wrapper classes?
1. Integer
2. Float
3. integer
4. character
5. Character
Answer
integer, character
Reason — All the names of wrapper classes begin with capital letters. Thus, Integer, Float
and Character are valid wrapper classes.
Question 5
The Wrapper class objects' value is comparable to primitive type values. True/false ?
Answer
True
Reason — With Autoboxing/unboxing feature, we can use the wrapper class object in the
same way as we use a primitive type data. Thus, the Wrapper class objects' value is
comparable to primitive type values.
Question 6
1. startsWith( )
2. random( )
Answer
1. boolean
2. double
Question 7
Answer
The Integer class has a String- and an int-constructor.
The Integer has a floatValue( ) method.
The Double class has constructors for type double and float.
Reason — The Integer class has a String- and an int-constructor as we can create Integer
objects by passing String and int type values at the time of object creation.
The Integer has a floatValue( ) method. The method float floatValue( ) returns the value
of the invoking object as a float primitive type.
The Double class has constructors for type double and float as we can create Double objects
by passing double and float type values at the time of object creation.
Question 8
class Output {
public static void main(String args[])
{
Integer i = new Integer(257);
byte x = i.byteValue();
System.out.print(x);
}
}
1. 0
2. 1
3. 256
4. 257
Answer
Reason — The byte datatype stores values in the range 0..255 and if we try to convert an
integer value bigger than 255 using byteValue( ) method then, the byte data type will convert
the value into the range of 0...255 in cyclic fashion. Thus, integer value 256 and 257 will be
converted to byte value 0 and 1 respectively.
Question 9
class Output
{
public static void main(String args[])
{
Integer i = new Integer(514);
float x = i.floatValue();
System.out.print(x);
}
}
1. 0
2. 1
3. 257
4. 514.0
Answer
514.0
Reason — The float data type stores decimal/floating point numbers. Thus, Integer
object 514 will be converted and stored as float value 514.0.
Assignment Questions
Question 1
Answer
Wrapper classes are specially designed classes that act as wrappers to primitive data types so
that primitive values can be accessed as objects.
For example, Integer is a wrapper class for int data type and Float is a wrapper class for float
data type.
Question 2
Answer
Question 3
What is the need of wrapper classes when there are primitive datatypes ?
Answer
The need of wrapper classes when there are primitive data types are as follows:
1. Java is an object oriented language where everything is used as objects. The wrapper
classes enable a primitive value to be used as objects. As objects, they can be used
with all types of classes and their methods.
2. Wrapper classes provide many ready-to-use utility methods such as converting a
string having primitive type value to equivalent primitive form. For example, "10" can
be converted to integer 10 using a wrapper class method.
3. Primitive data types are passed by value, but objects are passed by reference. Wrapper
classes facilitate passing primitives by reference, as an argument to a method, if so
required.
Question 4
Answer
The numeric wrapper class constructors may raise NumberFormatException at the time of
conversion of String arguments to primitive data types. The exception is raised when the
String argument cannot be converted to the desired data type.
For example,
Question 5
Name some methods that are commonly available in all wrapper classes and in all numeric
wrapper classes.
Answer
1. toString()
2. valueOf()
Question 6
What is autoboxing ? What is auto-unboxing ? How are these useful ?
Answer
The automatic conversion of primitive data type into an object of its equivalent wrapper class
is known as Autoboxing.
The automatic conversion of an object of wrapper class into primitive data type is known as
Auto-unboxing.
Question 7
Answer
The methods which return primitive values from Wrapper class objects are as follows:
Question 8
Answer
The methods which return Wrapper class objects from primitive values are as follows:
1. Byte.valueOf()
2. Short.valueOf()
3. Integer.valueOf()
4. Long.valueOf()
5. Float.valueOf()
6. Double.valueOf()
Question 9
Output
Explanation
Question 10
Question 11
double n2 = Double.parseDouble("2");
double n3 = Double.parseDouble("OCA");
System.out.println(n2 + " " + n3);
Answer
Question 12
double n1 = Double.parseDouble("8.0");
double n2 = Double.parseDouble("2");
System.out.println(n1 + " " + n2);
Answer
Output
8.0 2.0
Explanation
parseDouble() converts String arguments passed to it into double data type. Thus, 8.0 will be
assigned to n1 and 2.0 will be assigned to n2.
The values of n1 and n2 will be printed with a space " " between them.
Question 13
long a = 124235L;
Long b = new Long(a);
long c = b.longValue();
System.out.println(c);
Answer
Long b = new Long(a);
This statement boxes long type a into a Long object b, boxing a primitive type into a Wrapper
class object.
long c = b.longValue();
This statement unboxes Long object b and stores it in long variable c, converting a Wrapper
class object into a primitive data type.
Question 14
Integer c = 155;
Integer d = 155;
System.out.println(c == d);
System.out.println(c.equals(d));
Answer
Output
false
true
Explanation
c == d compares two Integer objects and returns false because when we compare two
objects using the operator "==", Java compares their reference i.e., their memory address and
not their value. Since these are two different objects stored at two different locations in
memory, Java produces result false
c.equals(d) method compares the values of the Integer objects after they are auto-unboxed
by the compiler and converted to primitive data types. The function returns true as both the
primitive type values are equal.
Question 15
In the following code, identify the statement where autoboxing is taking place :
Autoboxing is not taking place in this code as in the statement Integer i = new
Integer(10);, we are explicitly making an Integer object.
if(i < 100)
Auto-unboxing is taking place in this statement, as the value of an object cannot be compared
to a numeric value 100 directly but a primitive integer type value can be compared. So,
unboxing takes place before the value of i gets compared to 100.
System.out.println(i);
Auto-unboxing is happening in this statement as directly an object cannot be printed but a
primitive value can be printed. So, unboxing occurred before the value of i gets printed.
System.out.println(i + 10);
Auto-unboxing is taking place in this statement, as the value of an object cannot be used
with + operator like a primitive integer type value. Thus, i gets auto-unboxed to a primitive
type value and the value of i gets modified by the + operator.
Question 16
(i)
Output
35takes home2400
bmi : 22.913774881799036
(ii) Autoboxing is not happening in this code as the valueOf() method returns the Wrapper
class object. Thus, we are explicitly boxing the primitive type values to Wrapper objects in
the following statements: