Interview

20 Wrapper Class Interview Questions and Answers

Prepare for the types of questions you are likely to be asked when interviewing for a position where Wrapper Class will be used.

A Wrapper Class is a class that wraps (encapsulates) a primitive data type so that it can be used as an object. In Java, there are 8 primitive data types: byte, short, int, long, float, double, char, and boolean. By using a Wrapper Class, these data types can be given additional functionality, such as the ability to be added to a collection.

If you’re interviewing for a position that involves Java programming, you’re likely to be asked questions about Wrapper Classes. In this article, we’ll review some of the most common questions about Wrapper Classes and how you should answer them.

Wrapper Class Interview Questions and Answers

Here are 20 commonly asked Wrapper Class interview questions and answers to prepare you for your interview:

1. Can you explain what a wrapper class is in Java?

A wrapper class is a class that encapsulates a primitive data type so that it can be used as an object. For example, the primitive data type int can be wrapped in an Integer object. This is useful because it allows you to treat the primitive data type as an object, which means you can use it in places where only objects are accepted. It also allows you to perform certain operations on the data type that you couldn’t do with the primitive data type alone.

2. Why should we use wrapper classes instead of primitives?

Wrapper classes provide us with a number of benefits that primitives do not. For example, wrapper classes give us the ability to create objects, which means we can add them to collections and pass them as arguments to methods. Additionally, wrapper classes provide us with a number of methods that can be used to manipulate the data, such as the ability to convert the data to a different data type or format.

3. What are the different types of numeric wrapper classes available in Java?

There are eight different types of numeric wrapper classes available in Java:

– Byte
– Short
– Integer
– Long
– Float
– Double
– BigInteger
– BigDecimal

4. How do you create an object for a wrapper class?

You can create an object for a wrapper class by using the new keyword, followed by the name of the wrapper class. For example, to create an object for the Integer wrapper class, you would use the following code:

Integer myInt = new Integer(5);

5. How can you convert a String to a primitive type?

You can use the static method parseXXX() in the corresponding primitive wrapper class. For example, to convert a String to an int, you can use the static method parseInt() in the Integer class.

6. How can you convert a String to a wrapper class?

You can convert a String to a wrapper class using the static method parse() from the class you are converting to. For example, to convert a String to an Integer, you would use the Integer.parse() method.

7. When would you need to convert a String to a character array?

There are a few reasons you might need to convert a String to a character array. One reason is if you need to modify the characters in the String, and the String class does not provide a method to do what you need. Another reason is if you are working with a legacy API that expects a character array instead of a String.

8. What is the best way to convert a String to a primitive type value?

The best way to convert a String to a primitive type value is to use the static parse methods provided by the wrapper classes. For example, to convert a String to an int, you would use the static parseInt() method of the Integer class.

9. What do you understand by boxing and unboxing?

Boxing is the process of converting a value type into a reference type. Unboxing is the process of converting a reference type back into a value type.

10. Is it possible to convert a Double to a Boolean? If yes, then how?

Yes, it is possible to convert a Double to a Boolean. You can do this by using the Double.doubleToLongBits() method. This method returns a long value, which can then be converted to a Boolean using the Boolean.valueOf() method.

11. Is it possible to convert a string to a boolean? If yes, then how?

Yes, it is possible to convert a string to a boolean. This can be done by using the Boolean.parseBoolean() method.

12. What’s the difference between parseInt() and valueOf() methods? Which one would you prefer using?

The parseInt() method is used to convert a string into an integer, while the valueOf() method is used to convert an object into a string. I would prefer to use the parseInt() method when converting a string into an integer, as it is more explicit.

13. Are all wrapper classes immutable?

No, not all wrapper classes are immutable. The Boolean, Byte, Short, and Character wrapper classes are immutable, but the Integer, Long, Float, and Double wrapper classes are not.

14. What does the compareTo method do?

The compareTo method is used to compare two wrapper objects. The method returns 0 if the two objects are equal, a positive number if the first object is greater than the second object, and a negative number if the first object is less than the second object.

15. What do you understand about autoboxing?

Autoboxing is the automatic conversion of a primitive data type into its corresponding wrapper class. For example, converting an int into an Integer. This is useful because wrapper classes provide additional functionality that is not available with primitive data types, such as the ability to be null.

16. What happens if there is no matching wrapper class when converting from a primitive to a wrapper class?

If there is no matching wrapper class when converting from a primitive to a wrapper class, then the primitive will be autoboxed.

17. What are some common pitfalls with respect to working with wrapper classes?

One common issue is that people often forget that wrapper classes are objects, and thus they can be null. Another is that autoboxing can sometimes lead to unexpected results if people are not aware of how it works.

18. What is the significance of the equals() method?

The equals() method is used to compare two objects for equality. This is important because it allows you to determine if two objects are the same, even if they are of different types. For example, you could use the equals() method to compare an Integer and a Double object to see if they are equal.

19. Should we always override hashCode() and equals() methods together?

Yes, you should always override the hashCode() and equals() methods together. The hashCode() method is used to determine equality, so if two objects are equal, they must have the same hash code. The equals() method is used to compare two objects, and if they are equal, the hashCode() method is used to determine if they are the same object.

20. What happens if two objects have the same hash code but aren’t equal?

If two objects have the same hash code, they will be stored in the same bucket in a HashMap. However, they will still be considered two separate objects and will not be equal.

Previous

20 Bug Tracking Interview Questions and Answers

Back to Interview
Next

20 Oracle Discoverer Interview Questions and Answers