Java data types interview questions

What are the Wrapper classes available for primitive types?

boolean  -Z 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

Difference between long.Class and Long. TYPE?

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

What are wrapper classes?

They are wrappers to primitive data types. They allow us to access primitives as objects.

What is casting?

There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.

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

 boolean —> false

byte —-> 0

short —-> 0

int —–> 0

long ——> 0l

char —–> /u0000

float ——> 0.0f

double —-> 0.0d

any object reference —-> null

What is the advantage of using arrays over variables?

Arrays provide a structure wherein multiple values can be accessed using single reference and index. This helps in iterating over the values using loops.

What are the disadvantages of using arrays?

Arrays are of fixed size and have to reserve memory prior to use. Hence if we don’t know size in advance arrays are not recommended to use.

Arrays can store only homogeneous elements.

Arrays store its values in contentious memory location. Not suitable if the content is too large and needs to be distributed in memory.

There is no underlying data structure for arrays and no readymade method support for arrays, for every requirement we need to code explicitly

Advantage of Collection classes over Arrays ?

 Collections are re-sizable in nature. We can increase or decrease the size as per recruitment.

Collections can hold both homogeneous and heterogeneous data’s.

Every collection follows some standard data structures.

Collection provides many useful built in methods for traversing, sorting and search.

 

What are the Disadvantages of using Collection Classes over Arrays ?

Collections can only hold objects, It can’t hold primitive data types.

Collections have performance overheads as they deal with objects and offer dynamic memory expansion. This dynamic expansion could be a bigger overhead if the collection class needs consecutive memory location like Vectors.

Collections don’t allow modification while traversal as it may lead to concurrentModificationException.

What is the difference between Data Type and Data Structure?

Data type: a set of values together with operations on that type

Data structure: a physical implementation of a data type

Is it correct to say that Interfaces are abstract data types ?

No.

Data Type holds data whereas Interface doesn’t hold anything. Interface is a contract about how to communicate with the underlying Class.

Do you see Class as a Data Type or Data Structure?

Class can be better seen as Data Type. This could be implemented as a Data Structure too in some cases.