Arrays in Java

Arrays in Java

1.What is an array?
 A: Array is a group of related data items that share a common name.For instance, we can define an array name salary to represent a set of salaries of a group of employees.
Examples : salary[10]

2.How do we allocate an array dynamically in java?

A:Arrays in java are static lists that can store a certain kind of variables. Therefore these arrays need to be initialized at the compile time.
i.Though arrays offer efficient performance, they are not space efficient when the data grows to thousands of entries. In this case, it is always a better idea to allocate the memory dynamically. This is where a vector comes into picture.
ii.The key difference between Arrays and Vectors in Java is that Vectors are dynamically-allocated.
iii.Each Vector can contain a dynamic list of references to other objects. Therefore, they need not contain a single type of variable.
iv.The Vector class is found in the java.util package, and extends java.util.Abstractlist 

3.What is the difference between a Vector and an Array?

A:Differences between Vector and Array.

Vector is a growable and shrinkable where as Array is not.
Vector implements the List interface where as array is a primitive data type
Vector is synchronized where as array is not.
The size of the array is established when the array is created. As the Vector is growable, the size changes when it grows.

4.Explain with example how to initialize an array of objects.

A:An array can be instantiatd in various ways as follows:

int[] arr = new int[5];
int arr[] = new int[5];
int[] arr = Array.newInstance[int, 5];
int[] arr = {0,1,2,3,4};
int[][] arr2 = new int[length1][length2]; //2D array
list1 = new Object[5];       //An array of objects 

5.When would you use ArrayList and when LinkedList?

A:This is again one of the question where many of the interviewee got confused. Primarily, one would want to use ArrayList in the cases where there is a greater need to access the element rather than insertion or deletion. On the other hand, one would want to use the LinkedList when there is a greater need for insertion and deletion and not much use of the accessing the element at a particular index. This is mainly because the worst time complexity of accessing an element/object in an ArrayList is always “1? whereas in LinkedList it can be ”n”. In case of addition or deletion of element in an ArrayList, there is an operation such as System.arraycopy is involved. This is quite an expensive operation and thus, in usecases where frequent insertion and deletion of elements are involved, LinkedList is preferred where it is all about adding or removing the node and re-linking the existing node.

 6.While passing an ArrayList to a method or returning an ArrayList from a method, when is it considered to be a security violation? How to fix this problem?

A:When one passes the array to a method, if array is assigned to the member variable directly without making a copy of it, it could lead to a scenario that the original array when changed by the caller will also end up changing the array passed to the method. Look at the code below to see when it is security violation and subsequently how to fix the same.

7.How do you copy one ArrayList to another? Could you share the code?

A:Following are different techniques for copying an ArrayList object to another ArrayList:

i.Use clone() method such as following: ArrayList myObject = new ii.ArrayList<Object>(myTempObject);
iii.Use ArrayList constructor object such as following:
iv.use Collection.copy method

8.on which memory ,arrays are created in java?

A:Arrays are created on dynamic memory by JVM.There is no question of static memory in java everything is created on dynamic memory only.

9.can you call the main() method of a class from another class?

A:Yes,We can call the main() of a class from another class using Classname.main().At the time of calling the main() method,we should pass a string type array to it.

10.What is class name of java array?

A: In java, array is an object. For array object, an proxy class is created whose name can be obtained by getClass().getName() method on the object.

    class B{ 
    public static void main(String args[]){ 
    int arr[]={4,4,5}; 
    Class c=arr.getClass(); 
    String name=c.getName(); 

11.What are the different types of arrays?

A:There are two types of array.

i.Single Dimensional Array(1D array)
ii.Multidimensional Array(2D,3D...arrays)

i.One dimensional array is a list of variables of same type that are accessed by a common name. An individual variable in the array is called  an array element. Arrays forms a way to handle groups of related data.

Example of One dimensional array
Example : This example shows how to declare initialize  and display an array.

// Declaration of allocating memory to an array
int iarr[]  = new int[3];

// Initializing elements
iarr[0] = 1;
iarr[1] = 2;
iarr[2] = 3;

//Display array elements

// Or Use for loop to display elements
 for (int i = 0; i < iarr.length; i = i + 1)
       System.out.print(" ");

ii.Multidimensional Array

 The most basic multidimensional array is made of two dimensions. This is referred to as two-dimensional.  To create a two-dimensional array, inside of one pair of square brackets, use two pairs (of square brackets). The formula you would use is:

DataType[][] VariableName;


       class B{ 
    public static void main(String args[]){ 
    //declaring and initializing 2D array 
    int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; 
    //printing 2D array 
    for(int i=0;i<3;i++){ 
     for(int j=0;j<3;j++){ 
       System.out.print(arr[i][j]+" "); 


Post a Comment