In the last post, you have learned String programming interview questions but here we will see array coding interview questions in java.
Let's start programming questions on array.
(1) What is the output of following program?
class Demo1
{
public static void main(String args[])
{
int i[] = new int[0];
System.out.println(i[0]);
}
}
Output: java.lang.ArrayIndexOutOfBoundsException...run-time error
(2) Can we pass the negative number in array size declaration?
class Demo2
{
public static void main(String args[])
{
int i[] = new int[-3];
------
------
}
}
Output: java.lang.NegativeArraySizeException...run-time exception
(3) Write a java program to find intersection between two arrays?
In this example,we will find the common elements between 2 arrays.
class Demo3
{
public static void main(String args[])
{
String s1[] = {"red", "pink", "orange", "black"};
String s2[] = {"pink", "brown", "red", "white"};
//using HashSet class
HashSet<String> hs = new HashSet<String>();
for(int i = 0; i<s1.length; i++)
{
for(int j = 0; j<s2.length; j++)
{
if(s1[i].equals(s2[j]))
{
hs.add(s1[i]);
}
}
}
System.out.println(hs);
}
}
Output: [red, pink]
(4) Write a java program to sort an array elements?
By the help of Arrays.sort() method we can easily sort the array elements in ascending order and this sort() method internally uses quick sort algorithm to sort the elements of an array.
class Demo4
{
public static void main(String args[])
{
int[] i = {4, 1, 99, 3, 7};
Arrays.sort(i);
System.out.println(Arrays.toString(i));
}
}
Output: [1, 3, 4, 7, 99]
(5) Write a java program to reverse an array?
This is the simple java programs on array where we reverse the elements of an array in java.
class Demo5
{
public static void main(String args[])
{
int n[] = {1,2,3,4,5,6,7,8,9,10};
System.out.println("Array elements before reverse");
for(int i = 0; i<n.length; i++)
{
System.out.print(n[i]+" ");
}
for(int i = 0; i<n.length/2; i++)
{
int temp = n[i];
n[i] = n[n.length-1-i];
n[n.length-1-i] = temp;
}
System.out.println("\n Array elements after reverse");
for(int i = 0; i<n.length; i++)
{
System.out.print(n[i]+" ");
}
}
}
Output: Array elements before reverse
1 2 3 4 5 6 7 8 9 10
Array elements after reverse
10 9 8 7 6 5 4 3 2 1
(6) Write a java program to find duplicate elements in array?
class Demo6
{
public static void main(String args[])
{
String[] s = {"apple", "orange", "apple", "banana"};
HashSet<String> hs = new HashSet<String>();
for(String duplicate : s)
{
if(!hs.add(duplicate))
{
System.out.println(duplicate);
}
}
}
}
Output: apple
(7) Write a java program to find minimum and maximum value in an array?
class Demo7
{
public static void main(String args[])
{
int []a = {1,5,8,9,50,100,200};
//assign first element of an array to largest and smallest
int smallest = a[0];
int largest = a[0];
for(int i = 1; i<a.length; i++)
{
if(a[i]>largest)
largest = a[i];
else if(a[i]<smallest)
smallest = a[i];
}
System.out.println("Largest number is "+ largest);
System.out.println("Smallest number is "+ smallest);
}
}
Output: Largest number is 200
Smallest number is 1
(8) How to compare two arrays in java?
If two array are of the same size and data types then we can use Arrays.equals() method for comparison.
import java.util.*;
class Demo8
{
public static void main(String args[])
{
int a[] = {1, 2, 3, 4};
int b[] = {9, 8, 7, 6};
int c[] = {1, 2, 3, 4};
System.out.println(Arrays.equals(a, b));
System.out.println(Arrays.equals(b, c));
}