1. What is the primary purpose of wrapper classes in Java?
Answer:
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'?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The Character class is the wrapper class for the primitive 'char' type.
6. What is unboxing in Java?
Answer:
Explanation:
Unboxing is the conversion of a wrapper object to its corresponding primitive type.
7. How do you compare two wrapper objects for equality?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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.