Using Library Classes

ICSE Class 10 Computer Applications · Sumita Arora Computer Applications · 25 solved questions

Exercise
1Objective Type Questions

Which of these is a wrapper for data type int ?

Show SolutionHide Solution↓

Integer

Reason — Integer is a wrapper class for int data type.

2Objective Type Questions

Which of these is wrapper for simple data type char?

Show SolutionHide Solution↓

Character

Reason — Character is wrapper for simple data type char.

3Objective Type Questions

Which following method of wrapper Integer will convert the value of an object into int?

Show SolutionHide Solution↓

int intValue( )

Reason — int intValue( ) function returns the value of the invoking object as an int.

4Objective Type Questions

Which of the following is/are not valid wrapper classes?

Show SolutionHide Solution↓

integer, character

Reason — All the names of wrapper classes begin with capital letters. Thus, Integer, Float and Character are valid wrapper classes.

5Objective Type Questions

The Wrapper class objects' value is comparable to primitive type values. True/false ?

Show SolutionHide Solution↓

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.

6Objective Type Questions

Write the return data type of the following functions :

Show SolutionHide Solution↓
  1. boolean
  2. double
7Objective Type Questions

Which of the following statements are true ?

Show SolutionHide Solution↓

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.

8Objective Type Questions

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);
    }
}
Show SolutionHide Solution↓

1

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.

9Objective Type Questions

What is the output of this program ?

class Output
{
    public static void main(String args[]) 
    { 
        Integer i = new Integer(514);
        float x = i.floatValue(); 
        System.out.print(x);
    }
}
Show SolutionHide Solution↓

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.

1Assignment Questions

What are wrapper classes?

Show SolutionHide Solution↓

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.

2Assignment Questions

Name the numeric wrapper classes in Java.

Show SolutionHide Solution↓

The numeric wrapper classes in Java are:

  1. Byte for byte data type
  2. Short for short data type
  3. Integer for int data type
  4. Float for float data type
  5. Long for long data type
  6. Double for double data type
3Assignment Questions

What is the need of wrapper classes when there are primitive datatypes ?

Show SolutionHide Solution↓

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.
4Assignment Questions

When do the numeric wrapper class constructors raise NumberFormatException ?

Show SolutionHide Solution↓

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,

int val = Integer.parseInt("A");

Here, the String argument "A" cannot be converted to int type and therefore, NumberFormatException is thrown.

5Assignment Questions

Name some methods that are commonly available in all wrapper classes and in all numeric wrapper classes.

Show SolutionHide Solution↓

Methods available in all wrapper classes are:

  1. toString()
  2. valueOf()

Methods commonly available in all numeric wrapper classes are:

  1. xxxValue() methods — byteValue(), shortValue(), intValue(), longValue(), floatValue(), doubleValue().
  2. parseXXX methods — parseByte(), parseShort(), parseInt(), parseFloat(), parseLong(), parseDouble().
6Assignment Questions

What is autoboxing ? What is auto-unboxing ? How are these useful ?

Show SolutionHide Solution↓

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.

These are useful as:

  1. Autoboxing/auto-unboxing let us use primitive types and wrapper class objects interchangeably.
  2. It simplifies the process of converting between primitive types and their corresponding wrapper classes as the compiler does it automatically.
7Assignment Questions

Which methods return primitive values from Wrapper class objects?

Show SolutionHide Solution↓

The methods which return primitive values from Wrapper class objects are as follows:

  1. xxxValue() methods — byteValue(), shortValue(), intValue(), longValue(), floatValue(), doubleValue().
  2. parseXXX methods — parseByte(), parseShort(), parseInt(), parseFloat(), parseLong(), parseDouble().
8Assignment Questions

Which methods return Wrapper class objects from primitive values ?

Show SolutionHide Solution↓

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()
9Assignment Questions

Predict the output.

int res = Integer.valueOf("100").compareTo(new Integer(100)); 
System.out.println(res);
Show SolutionHide Solution↓
0

Here, Integer.valueOf("100") returns an Integer object storing 100 and new Integer(100) creates a new Integer object with the value 100.

Integer.compareTo() compares two Integer objects numerically, after auto-unboxing is performed by the compiler and the Integer objects are converted to integer type values. The function compareTo() returns 0 as primitive values of both the objects are equal.

10Assignment Questions

Find the error:

Integer obj = new Integer("A"); 
System.out.println(obj);
Show SolutionHide Solution↓

Integer obj = new Integer("A"); statement will generate NumberFormatException at the time of conversion of String argument "A" to Integer object. The exception is raised because "A" is not a valid string representation of a numeric value, therefore it cannot be converted to Integer object.

11Assignment Questions

Find the error :

double n2 = Double.parseDouble("2"); 
double n3 = Double.parseDouble("OCA"); 
System.out.println(n2 + " " + n3);
Show SolutionHide Solution↓

double n3 = Double.parseDouble("OCA"); statement will generate NumberFormatException at the time of conversion of String argument "OCA" to double data type. The exception is raised because "OCA" is not a valid string representation of a numeric value, therefore it cannot be converted to Integer object.

12Assignment Questions

Predict the output :

double n1 = Double.parseDouble("8.0"); 
double n2 = Double.parseDouble("2"); 
System.out.println(n1 + " " + n2);
Show SolutionHide Solution↓
8.0 2.0

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.

13Assignment Questions

What important is automatically happening in following code ?

long a = 124235L; 
Long b = new Long(a);
long c = b.longValue(); 
System.out.println(c);
Show SolutionHide Solution↓

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.

14Assignment Questions

Predict the output :

Integer c = 155; 
Integer d = 155;
System.out.println(c == d); 
System.out.println(c.equals(d));
Show SolutionHide Solution↓
false
true

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.

15Assignment Questions

In the following code, identify the statement where autoboxing is taking place :

Integer i = new Integer(10);
if (i < 100)
    System.out.println(i); 
else
    System.out.println(i + 10);
Show SolutionHide Solution↓

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.

16Assignment Questions

From the following code, do the following :

(i) find its output.

(ii) find out the statements where autoboxing is taking place.

(iii) find out the statements where auto-unboxing is taking place.

Short age = Short.valueOf("35");
Integer salary = Integer.valueOf("2400"); 
Float height = Float.valueOf("1.78");   // in meters
Double weight = Double.valueof ("72.6"); 
double b = weight / (height * height);
System.out.println(age + "takes home" + salary); 
System.out.println("bmi : " + b);
Show SolutionHide Solution↓

(i)

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:

Short age = Short.valueOf("35");
Integer salary = Integer.valueOf("2400"); 
Float height = Float.valueOf("1.78");   // in meters
Double weight = Double.valueof ("72.6"); 

(iii) Auto-unboxing is taking place in the following statements:

  1. double b = weight / (height * height);
    The Wrapper class objects weight and height are being auto-unboxed before mathematical operations can be performed on them.
  2. System.out.println(age + "takes home" + salary);
    The Wrapper class objects age and salary are being auto-unboxed before they can be printed as objects cannot be printed directly like primitive data type values.

Press ↓ to reveal a solution and move to the next question. Back to Sumita Arora Computer Applications.

Free sample bank