Monday, January 11, 2016

Top 5 Java Arrays Interview Questions and Answers with Examples

1) How can you find Duplicate elements in an Array?

Ans: This Question very Frequently ask in the Interview for Freshers. Interviewer wants to check your logical and programming skills. We find Duplicate elements in an array by using HashSet  in java. Because HashSet does not allow duplicate elements. In this class there is add() method to add elements  of an array into HashSet.If an element is add succssfully then it returns true otherwise false.

Example:

Import java.util.*;
class FindDuplicate{
public static void main(String args[])
{
String [] strArray={"lucky","abc","lucky","reddy","pqr"};
HashSet<String> hs=new HashSet<String>();
for(String arryElement:strArray)
{
if(!hs.add(arryElement))
{
System.out.println("duplicate element is"+arryElement);
}
}
}
}

Output: 
duplicate element is lucky

There is another method also avaliable to find duplicate elements in java that is Brute Force method. This method Performance wise very slow because while finding duplicate elements it compares the each element of an array with other element.So while developing Java Projects it is not recommended to use.



2) How can you copy one ArrayList to another ArrayList?Explain with example?

Ans: we can  copy one ArrayList Object to another In Java by using 3 different Techniques.
1) Use Clone method

Example:

public class ArrayCloneDemo
{
public static void main(String args[])
{
int arr[]={11,12,13,14,15};
int arry[]=arr;//here copying the reference arr to array
arry[2]=5;

//both give same result because arr and array are the reference of same array then no matter which reference is used to update values

System.out.println(arry[2]);
System.out.println(arr[2]);

//now introduce clone() method

System.out.println("*******");
arry=arr.clone();//arr.clone() creates a separate copy of the array in memory and then assign it to arry.now both arr and arry point to two different arrays so changes made to one will not impact the other.

arry[1]=3;
System.out.println(arry[1]);
System.out.println(arr[1]);  // here both prints result stored in the first index in the array they point to..
}
}

Output:
5
5
*******
3  
12

2) Using Collections.copy method
3) using Array List Constructor Object.

Note: Here Clone() method create shallow copy.

Also Read The Post:  ArrayList in java

3) When we should go for Array List and when Linked List?

Ans:  This is another Frequently asked in Technical Interview. This question will ask for Experienced candidates because Interviewer wants to know where does he used ArrayList and Linked List in his Project.

ArrayList is used where there is greater need to access the elements rather than insertion or deletion.Whereas Linked List is used when you want insert or delete elements as not much use of accessing elements at a particular index.

4) How to iterate over array in Java?

Ans:
By using for loop we can iterate over array in java.

Example:

class iterateDemo{
public static void main(String args[])
{
int arr[]=new int[]{33,44,55,66,77};
for(int i:arr)
{
System.out.println(i);
}
}

5) How can You Sort the array elements in java?

Ans:
You can sort array elements in java by using Arrays.sort(). This method internally use Quick Sort algorithm.

Example:

import java.util.*;
public class SortArrayDemo
{
public static void main(String args[])
{
int arr[]=new int[]{66,44,55,33,77};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));

Output:
33
44
55
66
77


I hope you enjoy this post and share to your friends and also in social websites,write comments on this topic and keep follow me for latest updates.











No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...