Java Wrapper Classes MCQ

1. What is the primary purpose of wrapper classes in Java?

a) To provide a way to use primitive types as objects
b) To wrap text data
c) To encapsulate complex data structures
d) To improve performance of collections

Answer:

a) To provide a way to use primitive types as objects

Explanation:

Wrapper classes in Java provide a way to use primitive data types (int, char, etc.) as objects.

2. Which of the following is a wrapper class for the primitive type 'int'?

a) Integer
b) Int
c) IntObject
d) IntWrapper

Answer:

a) Integer

Explanation:

The Integer class is the wrapper class in Java for the primitive type 'int'.

3. How do you convert a primitive type to its corresponding wrapper object?

a) Using wrapper constructors
b) Using static conversion methods
c) Directly assigning a primitive to a wrapper variable
d) All of the above

Answer:

d) All of the above

Explanation:

Primitive types can be converted to wrapper objects using wrapper constructors, static conversion methods, or auto-boxing.

4. What is auto-boxing in Java?

a) Automatically wrapping a primitive type in its corresponding wrapper class
b) Boxing data into a collection
c) Converting objects to primitive types
d) A method of data encryption

Answer:

a) Automatically wrapping a primitive type in its corresponding wrapper class

Explanation:

Auto-boxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.

5. What is the wrapper class for the 'char' primitive type?

a) Char
b) Character
c) CharObject
d) CharSequence

Answer:

b) Character

Explanation:

The Character class is the wrapper class for the primitive 'char' type.

6. What is unboxing in Java?

a) Removing an object from a collection
b) Converting a wrapper object to its corresponding primitive type
c) Removing encryption from data
d) Unwrapping a string value

Answer:

b) Converting a wrapper object to its corresponding primitive type

Explanation:

Unboxing is the conversion of a wrapper object to its corresponding primitive type.

7. How do you compare two wrapper objects for equality?

a) Using the == operator
b) Using the equals() method
c) Both a and b
d) By converting them to primitive types first

Answer:

b) Using the equals() method

Explanation:

To compare two wrapper objects for value equality, the equals() method should be used. The == operator compares object references.

8. Which wrapper class is used for the 'boolean' primitive type?

a) Boolean
b) Bool
c) BooleanObject
d) BooleanWrapper

Answer:

a) Boolean

Explanation:

The Boolean class is the wrapper class for the primitive type 'boolean'.

9. What happens when you assign a null value to a primitive type during unboxing?

a) The primitive type takes a default value
b) A compile-time error occurs
c) A runtime exception is thrown
d) The primitive type becomes null

Answer:

c) A runtime exception is thrown

Explanation:

Assigning a null value to a primitive type during unboxing results in a NullPointerException.

10. Which of the following is a valid way to instantiate a wrapper class object?

a) Integer i = new Integer(5);
b) Integer i = Integer.valueOf(5);
c) Integer i = 5; // auto-boxing
d) All of the above

Answer:

d) All of the above

Explanation:

A wrapper class object can be instantiated using the constructor, static factory methods, or auto-boxing.

11. How do you obtain the primitive value from a wrapper object?

a) Using value methods like intValue(), charValue()
b) Using toString() method
c) By direct assignment to a primitive variable
d) Both a and c

Answer:

d) Both a and c

Explanation:

The primitive value can be obtained from a wrapper object using value methods like intValue() or through unboxing by direct assignment.

12. Can wrapper classes be used with collections in Java?

a) Yes
b) No
c) Only certain collections
d) Only with additional libraries

Answer:

a) Yes

Explanation:

Wrapper classes are used with collections in Java to store primitive types as objects.

13. What is the output of comparing two Integer objects with the same value using the == operator?

a) true
b) false
c) Depends on the values
d) Compilation error

Answer:

b) false

Explanation:

The == operator compares references, not values, so it returns false unless both references point to the same object.

14. Which of these is not a wrapper class in Java?

a) Float
b) Short
c) String
d) Byte

Answer:

c) String

Explanation:

String is not a wrapper class; it is a class for character strings. Float, Short, and Byte are all wrapper classes.

15. What is the benefit of using wrapper classes in Java?

a) Increased performance
b) Lower memory usage
c) Ability to use primitive types in generic collections
d) Automatic conversion to strings

Answer:

c) Ability to use primitive types in generic collections

Explanation:

One of the main benefits of wrapper classes is that they allow primitive types to be used in generic collections like ArrayList, where only objects can be stored.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top