Java - Data Types / Wrapper Classes - Interview Questions and Answers on Integer and Long

Q1.  What are the Wrapper classes available for primitive types ?

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

Q2.  Difference between long.Class and Long.TYPE ?

Ans. They both represent the long primitive type. They are exactly the same.

Q3.  What are the default or implicitly assigned values for data types in java ?

Ans. boolean ---> false
byte ----> 0
short ----> 0
int -----> 0
long ------> 0l
char -----> /u0000
float ------> 0.0f
double ----> 0.0d
any object reference ----> null

Q4.  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.

Q5.  How to find whether a given integer is odd or even without use of modules operator in java?

Ans. public static void main(String ar[])
{
     int n=5;
     if((n/2)*2==n)
     {
          System.out.println("Even Number ");
     }
     else
     {
          System.out.println("Odd Number ");
     }
}
}

Q6.  Is it legal to initialize List like this ?

LinkedList<Integer> l=new LinkedList<int>(); 

Ans. No, Generic parameters cannot be primitives.

Q7.  What will the following code print ?

public static void main(String[] args){
Integer i1 = new Integer("1");
        Integer i2 = new Integer("2");
        Integer i3 = Integer.valueOf("3"); 
        int i4 = i1 + i2 + i3; 
        System.out.println(i4);  
}

Ans. 6

Q8.  Which of the following syntax are correct ?

a. LinkedList<Integer> l=new LinkedList<int>();
b. List<Integer> l=new LinkedList<int>();
c. LinkedList<Integer> l=new LinkedList<Integer>();
d. List<Integer> l = new LinkedList<Integer>();

Ans. c and d are correct.

Q9.  Can we compare Integers by using equals() in Java ?

Ans. Yes for the Wrapper class Integer but not for the primitive int.

Q10.  Is this valid in Java ?

Long x = new Long ("42");

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.

Q11.  What is the size of long data type ?

 a. 16 bit
 b. 32 bit
 c. 64 bit
 d. 128 bit

Ans. 64 bit

Q12. Which interfaces are implemented by  Integer?

Ans.[Comparable]

Q13. What is the package name for Long class?

Ans.java.lang

Q14. Which is the Parent Class of Long class?

Ans.java.lang.Number