Java Assignments, Primitive and Wrapper Classes Interview Questions

What are the different primitive data types supported in Java?

There are 8 primitive data types supported by Java. Apart from below 8, everything is object in Java.

  1. int
  2. byte
  3. short
  4. boolean
  5. double
  6. char
  7. float

What are the default values of object and primitve types. When is it necessary to assign values?

Default values of any object type is null. If the variables are defined as instance variables then a default value is assigned but for any local variable default values are not assigned by default and we have to initialize it explicitly. Without initialization, we cannot use the local variables and doing so will give compile time error. Following are the default values of primitive data type-

  1. byte, short, int, long – 0
  2. float, double- 0.0
  3. boolean – false
  4. char- ‘\u000’

In how many ways an integer value can be represented?

There are three ways to represent integer value –

  1. Decimal – it is a base 10 representation. This is default format.
  2. Octal – it is a base 8 representation. We need to use prefix 0 with values.
  3. HexaDecimal – it is a base 16 representation. We need to use case insensitive  prefix 0x.

Explain primitive casting?

Primitive casting is casting a primitive data type to another primitive data type. Primitive casting can be implicit or explicit.

Explain implicit and explicit primitive casting?

Implicit primitive casting is implicit and we need not mention cast. This happens when we cast a small thing to big. On the contrary explicit variable is explicit and we have to cast explicitly when a big type needs to be assigned to a small type.

What do you mean by wrapper classes?

Answer-  Corresponding to each primitive data type there is a wrapper class. Wrapper class is wrapping a primitive type to get an object. Following is the list of wrapper classes-

  1. boolean-             java.lang.Boolean
  2. byte-                   java.lang.Byte
  3. char-                   java.lang.Character
  4. double-               java.lang.Double
  5. float-                   java.lang.Float
  6. int-                      java.lang.Integer
  7. long-                   java.lang.Long
  8. short-                  java.lang.Short

How to convert a primitive type to wrapper class?

There are two ways in which we can convert primitive data type to a wrapper objects.

  1. Constructor- Every wrapper class constructor takes the primitive value and returns corresponding wrapper object.
  2. Utility Class- Every type has a utility classes which exposes static methods valueOf()  and takes the primitive value like Float.valueOf(“2.3f”)

How to convert a wrapper object to primitive data type?

Primitive values can be obtained from wrapper classes by calling xxxValue() methods of wrapper class where xxx is primitive data type like

Integer it = new Integer(23);

int primitive= it.intValue(); 

Like us on Facebook