Core Java - Interview Questions and Answers for 'Wrapper classe' - 13 question(s) found - Order By Newest Q1. Will this code give error if i try to add two heterogeneous elements in the arraylist. ? and Why ? List list1 = new ArrayList<>(); list1.add(5); list1.add("5");
Ans. If we don't declare the list to be of specific type, it treats it as list of objects. int 1 is auto boxed to Integer and "1" is String and hence both are objects. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   arraylist   list   autoboxing   wrapper classes expert   rare Q2. What are the Wrapper classes available for primitive types ? Core Java
Ans. boolean - java.lang.Boolean
byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   java5   data types   wrapper classes   adapter design pattern   rare Ans. They are wrappers to primitive data types. They allow us to access primitives as objects. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   data types   wrapper classes Asked in 3 Companies basic   frequent Q4. What are the benefits of using Wrapper classes over primitive types ? Core Java
Ans. With Collection classes , we cannot use primitive types. Moreover for any class using generic types, we cannot use primitive types.
They add more functionality by means of additional methods.
As their reference can be null , they offer consistent check for uninitialized state.
They facilitate caching and reuse by means of constant Pools. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  wrapper classes  benefits of wrapper classes over primitivesAns. Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   java5   autoboxing   wrapper classes Asked in 2 Companies basic   frequent Q6. Difference between boolean and Boolean ? Core Java
Ans. boolean is a primitive type whereas Boolean is a class. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops   wrapper classes   boolean vs Boolean basic   rare Q7. What are Wrapper Classes ? What are Primitive Wrapper Classes ? Core Java
Ans. A wrapper class is any class which "wraps" or "encapsulates" the functionality of another class or component. A Wrapper Class that wraps or encapsulates the primitive data type is called Primitive Wrapper Class. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   wrapper classes   primitive wrapper classes Q8. What Design pattern Wrapper Classes implement ? Design
Ans. Adapter. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   wrapper classes   adapter design pattern   design pattern Q9. Can we compare Integers by using equals() in Java ? Core Java
Ans. Yes for the Wrapper class Integer but not for the primitive int. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   wrapper classes   equals Q10. Is this valid in Java ? Long x = new Long ("42"); Core Java
Ans. Yes. Long wrapper class has overloaded constructor which takes String as input and then translate it to the long value and stored it as long. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   wrapper classes   long wrapper class   longAns. Boxing conversion converts expressions of primitive type to corresponding expressions of reference type.
For example
boolean to Boolean
long to Long
double to Double Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  data type  boxing  wrapper classes Q12. Does Wrapper classes produces immutable objects ? Core Java
Ans. Yes Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  wrapper classes  immutable  immutability Q13. Is immutability an advantage with Wrapper classes over primitive types ? Core Java
Ans. There is no concept of de referencing with primitive types and hence they are implicitly immutable. Having wrapper classes as mutable offers disadvantages compared to primitive types. Wrapper classes being immutable offer similar advantage as primitive types.It actually overshadows the disadvantage wrapper class could have if they are immutable. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  immutable  immutability classes  wrapper classes