Object Oriented Programming using Java Questions and Answers – Wrapper Class

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Wrapper Class”.

1. What is the output of the following Java code?

import java.util.ArrayList; 
class ArrayList 
{ 
    public static void main(String[] args) 
    { 
        ArrayList<Integer> al = new ArrayList<Integer>(); 
        al.add(25); 
        System.out.println(al.get(0)); 
    } 
}

a) 25
b) Garbage Value
c) Compilation Error
d) -1
View Answer

Answer: a
Explanation: The above program uses the concept of wrapper class. When working with Collection objects and data structures such as ArrayList, primitive types cannot be used. Hence, wrapper class ‘Integer’ is used in this case.
Output:

$ javac ArrayList.java
$ java ArrayList
25

2. What is the output of the following Java code?

advertisement
advertisement
import java.util.ArrayList; 
class ArrayList2 
{ 
    public static void main(String[] args) 
    { 
        ArrayList<int> al = new ArrayList<int>(); 
        al.add(25); 
        System.out.println(al.get(0)); 
    } 
}

a) 25
b) Garbage Value
c) Compilation Error
d) -1
View Answer

Answer: c
Explanation: The above program uses the concept of wrapper class. When an object to a wrapper class is created, a memory location is allocated and primitive data types can be stored. As ArrayList uses only object reference for storing values, primitive datatypes like ‘int’ cannot be used.
Output:

$ javac ArrayList2.java
$ java ArrayList2
Uncompilable source code - unexpected type
required: reference
found:    int

3. What is the output of the following Java code?

advertisement
class IntegertoString 
{
    public static void main(String[] args)
    {
        Integer num = 12345;
        String str = num.toString();
        System.out.println(str.length());
    }
}

a) 5
b) Compilation error
c) 4
d) Null
View Answer

Answer: a
Explanation: Wrapper classes come under java.util package. ‘toString()’ method is used to convert wrapper objects to strings. In the following example, we convert an Integer object to a String, and use the length() method of the String class to output the length of the “string”.
Output:

advertisement
$ javac IntegertoString.java
$ java IntegertoString
5

4. What is the output of the following Java code?

class InttoString 
{
    public static void main(String[] args)
    {
        int num = 150;
        String str = num.toString();
        System.out.println(str.length());
    }
}

a) 3
b) Compilation error
c) 150
d) 2
View Answer

Answer: b
Explanation: A wrapper class in Java converts a primitive data type into class object. toString() method is used to convert wrapper objects to strings but as num is a primitive datatype(int), an error is thrown because toString() method cannot convert a primitive datatype to a string.
Output:

$ javac InttoString.java
$ java InttoString
Uncompilable source code

5. What is the output of the following Java code?

class InttoDouble 
{
    public static void main(String[] args) 
    {    
        Integer myInt = 4;
        Double myDouble = 3.99;
        System.out.print(myInt+" ");
        System.out.println(myDouble);
    }
}

a) 4 3.99
b) 4 3
c) 4 0.99
d) Compilation error
View Answer

Answer: a
Explanation: Wrapper classes are those whose objects wraps a primitive data type within them. In the java.lang package, Java provides a separate class for each of the primitive data types namely Byte, Character, Double, Integer, Float, Long and Short. The objects can be printed directly using System.out.print() function.
Output:

$ javac InttoDouble.java
$ java InttoDouble
4 3.99

6. What is the output of the following Java code?

class Double 
{
    public static void main(String[] args) 
    {
        Double num = 3;
        System.out.println(num);
    }
}

a) 3
b) 3.0
c) Garbage Value
d) Compilation Error
View Answer

Answer: d
Explanation: Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects. In the java.lang package, Java provides a separate class for each of the primitive data types namely Byte, Character, Double, Integer, Float, Long, Short. As the object ‘num’ is of object Double, it needs to have a fractional part to be differentiated from an Integer object. In this code, 3 is considered an Integer object and not as a Double object.
Output:

$ javac Double.java
$ java Double
Uncompilable source code

7. What is the output of the following Java code?

class Double2
{
    public static void main(String[] args) 
    {
        Double num = 6.80;
        System.out.println(num);
    }
}

a) 6.80
b) 6.8
c) Garbage Value
d) Compilation Error
View Answer

Answer: b
Explanation: The wrapper class in Java provides the mechanism to convert primitive datatypes into object and object into primitive. The ‘num’ object has the value 6.80 which makes it readable by the compiler as a object of type Double without any ambiguity. Compilers ignores the zero at the least valued position.
Output:

$ javac Double2.java
$ java Double2
6.80

8. What is the output of the following Java code?

class PiObject
{
    public static void main(String[] args) 
    {
        Double num1 = 3.14;
        Double num2 = new Double(1.42);
        System.out.print(num1+" "+num2);
    }
}

a) 3.14 1.42
b) Compilation error
c) 3.14 null
d) 3.14 -1
View Answer

Answer: a
Explanation: Both the statements written above convey the same thing, ie converting a primitive data to an object. The primitive data types are not objects and hence they do not belong to any class like the Wrapper classes. While using collections or data structures such as ArrayList, it is required to convert the primitive type to object first which we can do by using wrapper classes.
Output:

$ javac PiObject.java
$ java PiObject
3.14 1.42

9. What is the output of the following Java code?

class Value
{  
    public static void main(String args[])
    {  
        Integer obj = new Integer(100);  
        int num = obj.intValue();
        System.out.println(num+ " "+ obj);  
    }
}

a) 100 D
b) 100 100
c) Compilation error
d) Garbage value
View Answer

Answer: b
Explanation: A Wrapper class is a class whose object wraps or contains a primitive data type. We can wrap a primitive value into a wrapper class object. An object of a wrapper class can also be converted to its corresponding primitive type. In this case, an Integer object is converted to int using intValue().
Output:

$ javac Value.java
$ java Value
100 100

10. What is the output of the following Java code?

class Calculate 
{ 
    public static void main(String[] args) 
    { 
        Integer obj = new Integer(10);  
        calc(obj);
        System.out.print(obj);
    }  
    public static void calc(Integer obj) 
    { 
        obj = obj + 1;     
    } 
}

a) 10
b) 11
c) Compilation error
d) -1
View Answer

Answer: a
Explanation: Wrapper classes are immutable in nature. An object of a wrapper class can also be converted to its corresponding primitive type. Hence, the wrapper classes cannot be modified and the same value of the object stays till the end.
Output:

$ javac Calculate.java
$ java Calculate
10

11. What is the output of the following Java code?

class ValueOfNum
{  
    public static void main(String args[])
    {  
        int num=10;  
        Integer obj=Integer.valueOf(num);  
        System.out.println(num+ " "+ obj);  
    }
}

a) 10 10
b) 10 -1
c) Garbage value
d) Compilation error
View Answer

Answer: a
Explanation: Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects. Here an int is converted into an Integer. Indirectly, a primitive datatype is converted into an object.
Output:

$ javac ValueOfNum.java
$ java ValueOfNum
10 10

12. What is the output of the following Java code?

class CompareTo
{  
    public static void main(String args[])
    {  
        Integer i = new Integer(10); 
        System.out.println(i.compareTo(7));    
    }
}

a) 1
b) 0
c) -1
d) Compilation error
View Answer

Answer: a
Explanation: ‘compareTo’ method in java.lang.Number class is used to compare two objects of same type. It is an additional feature under Number class for wrapper classes. Two different types cannot be compared, so both the argument and the Number object that invoke the method should be of the same type. The value 1 is returned if the number is greater than the argument.
Output:

$ javac CompareTo.java
$ java CompareTo
1

13. What is the output of the following Java code?

class Conversion
{  
    public static void main(String args[])
    {  
        int b=100;
        System.out.print(Integer.toHexString(b)+" "); 
        System.out.print(Integer.toOctalString(b)+" "); 
        System.out.print(Integer.toBinaryString(b)+" ");    
    }
}

a) 64 144 1100100
b) 64 134 0010011
c) 59 144 1100100
d) 64 134 0010011
View Answer

Answer: a
Explanation: A wrapper class is a class whose object wraps or contains a primitive data types. The above methods are used to convert integers of base 10 into hexadecimal, octal and binary type.
Output:

$ javac Conversion.java
$ java Conversion
64 144 1100100

14. What is the output of the following Java code?

class StringValueOf
{  
    public static void main(String args[])
    {  
        String b="100";
        System.out.print(Integer.valueOf(b,2));  
    }
}

a) 1100100
b) 4
c) 100
d) Compilation error
View Answer

Answer: b
Explanation: The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive. The valueOf() returns an Integer object holding the integer value of the specified string representation, parsed with the value of radix. In this case, 2 is the base radix. Hence, 100 is converted into binary.
Output:

$ javac StringValueOf.java
$ java StringValueOf
4

15. What is the output of the following Java code?

class IndexOf
{  
    public static void main(String args[])
    {  
        Integer b=17;
        String str=Integer.toString(b);
        System.out.println(str.indexOf("5"));
    }
}

a) 2
b) 17
c) Compilation error
d) -1
View Answer

Answer: d
Explanation: Wrapper classes are those whose objects wraps a primitive data type within them. Here, variable ‘b’ is of a primitive data type int. It is converted to a string using a toString() method and hence all string functions can be used on it.
Output:

$ javac IndexOf.java
$ java IndexOf
-1

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

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.