Preview

15 - Wrapper Classes: Integer and Double

 1. Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.

  TRUE

  FALSE

 2. Analyse the following code which uses wrapper classes. To create a wrapper object, use the wrapper class instead of the ___________.
public class MyClass { 
  public static void main(String[] args) { 
    Integer myInt = 5; 
    Double myDouble = 5.99; 
    Character myChar = 'A'; 
    System.out.println(myInt);
    System.out.println(myDouble);
    System.out.println(myChar);
  }
}

  wrapper object

  string object

  variable

  primitive type

 3. The advantage of using wrapper classes is that you can then have access to all the associated ______ that correspond to the object. E.g. intValue(), byteValue()

  methods

  attributes

  variables

  properties

 4. In the following example an Integer is converted to a String. What is the final output?
public class MyClass { 
  public static void main(String[] args) { 
    Integer myInt = 100; 
    String myString = myInt.toString();
    System.out.println(myString.length());
  }
}

  myInt=0

  100

  3

  Error

 5. The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, byte to Byte, char to Character, int to Integer

  True

  False

 6. The following code shows examples of 'unboxing' which involves:
byte bytevalue=byteobj;  
short shortvalue=shortobj;  
int intvalue=intobj;  
long longvalue=longobj;  
float floatvalue=floatobj;  
double doublevalue=doubleobj;  
char charvalue=charobj;  
boolean boolvalue=boolobj;  

  converting objects to primitives

  converting strings to integers

  converting primitives to objects

  converting matrices into arrays

 7. Read the following statements. Are both of them correct? (if so, select 'True')
1.None of the primitive data types has a dedicated class in java library which is why they require wrapper classes.
2. The wrapper class provides many methods that are useful while using collections, including provision for sorting, searching etc.

  TRUE

  FALSE

 8. What does the following code do?
double b = 55.05;
String str = "45";
Double x = new Double(b);
Double y = new Double(str);

  constructs two double objects

  constructs two variables as classes, x and y

  constructs a character class and a str class

  constructs two double classes

 9. Are both the following statements correct? (select 'True' if so)
1 - Integer(int value) - Constructs a newly allocated Integer object that represents the specified int value.
2 - Integer(String s) - Constructs a newly allocated Integer object that represents the int value indicated by the String parameter.

  FALSE

  TRUE

 10. Analyse the following code. What is the output of decode(45)?
private static void decodeMethods() {

 // decode() : decodes the hex,octal and decimal
 // string to corresponding int values.
 String decimal = "45";
 String octal = "005";
 String hex = "0x0f";
 Integer dec = Integer.decode(decimal);
 System.out.println("decode(45) = " + dec);
 dec = Integer.decode(octal);
 System.out.println("decode(005) = " + dec);
 dec = Integer.decode(hex);
 System.out.println("decode(0x0f) = " + dec);

}

  5

  10

  45

  500

 11. What is the output of the following code?
class stringToInt
{
     public static void main(String args[])
     {
String s = "123";
Integer intObj = Integer.valueOf(s);
  int i = obj.intValue();
i += 2;
System.out.println(i);
     }
}

 12. There is a wrapper class for every primitive in Java. For instance, the wrapper class for int is ______.

  intClass

  classint()

  Class-int

  Integer

 13. All of the wrapper classes except ________ provide two constructors: one that takes a primitive of the type being constructed, and one that takes a String representation of the type being constructed-

  Character

  Float

  Boolean

  Double

 14. The Character class provides only ___ constructor, which takes a char as an argument-e.g. Character c1 = new Character('c');

  minimal

  a bit type

  a NULL

  one

 15. Wrapper classes are useful in storing primitive data types in higher level data structures

  TRUE

  FALSE