Arrays are one of the most commonly asked topics in Java interviews. They form the foundation for understanding data structures and algorithms. Whether you’re preparing for a beginner-level Java test or a professional coding interview, you’ll often encounter questions based on array manipulation — searching, sorting, reversing, and more.

In this blog post, we’ll go through important array programs in Java that are frequently asked in interviews. Each example includes code, logic explanation, and expected output so you can practice confidently.

What is an Array in Java?

An array in Java is a collection of elements of the same type stored in a contiguous memory location.
It allows you to store multiple values under a single variable name instead of declaring separate variables for each value.

Example:


int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[2]); // Output: 30

Arrays can be one-dimensional, two-dimensional, or multidimensional depending on how the data is structured.

1. Program to Print Elements of an Array

Code:


public class PrintArray {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        System.out.println("Array elements:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}

Output:
Array elements: 1 2 3 4 5

Explanation: This program uses a for-each loop to print each element of the array.

2. Program to Find the Sum of Array Elements

Code:


public class SumArray {
    public static void main(String[] args) {
        int[] arr = {5, 10, 15, 20};
        int sum = 0;
        for (int num : arr) {
            sum += num;
        }
        System.out.println("Sum = " + sum);
    }
}

Output:
Sum = 50

Explanation: We initialize sum to 0 and add each element of the array inside the loop.

3. Program to Find the Largest Element in an Array

Code:


public class LargestElement {
    public static void main(String[] args) {
        int[] arr = {25, 78, 56, 89, 102};
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) { if (arr[i] > max)
                max = arr[i];
        }
        System.out.println("Largest element: " + max);
    }
}

Output:
Largest element: 102

4. Program to Find the Smallest Element in an Array

Code:


public class SmallestElement {
    public static void main(String[] args) {
        int[] arr = {25, 78, 56, 89, 102};
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < min)
                min = arr[i];
        }
        System.out.println("Smallest element: " + min);
    }
}

5. Program to Reverse an Array

Code:


public class ReverseArray {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50};
        System.out.println("Reversed Array:");
        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.print(arr[i] + " ");
        }
    }
}

Output:
Reversed Array: 50 40 30 20 10

6. Program to Copy Elements of One Array into Another

Code:


public class CopyArray {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5};
        int[] arr2 = new int[arr1.length];
        for (int i = 0; i < arr1.length; i++) {
            arr2[i] = arr1[i];
        }
        System.out.println("Copied elements:");
        for (int num : arr2) {
            System.out.print(num + " ");
        }
    }
}

7. Program to Sort an Array in Ascending Order

Code:


import java.util.Arrays;

public class SortArray {
    public static void main(String[] args) {
        int[] arr = {5, 2, 8, 1, 3};
        Arrays.sort(arr);
        System.out.println("Sorted array:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}

Output:
Sorted array: 1 2 3 5 8

8. Program to Search an Element in an Array

Code:


public class SearchArray {
    public static void main(String[] args) {
        int[] arr = {5, 10, 15, 20, 25};
        int target = 15;
        boolean found = false;

        for (int num : arr) {
            if (num == target) {
                found = true;
                break;
            }
        }

        if (found)
            System.out.println(target + " found in array.");
        else
            System.out.println(target + " not found in array.");
    }
}

9. Program to Find Duplicate Elements in an Array

Code:


public class DuplicateElements {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 2, 4, 3, 5};
        System.out.println("Duplicate elements:");
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] == arr[j]) {
                    System.out.println(arr[j]);
                }
            }
        }
    }
}

10. Program to Count Even and Odd Numbers in an Array

Code:


public class EvenOddCount {
    public static void main(String[] args) {
        int[] arr = {2, 5, 8, 9, 12, 15};
        int even = 0, odd = 0;
        for (int num : arr) {
            if (num % 2 == 0)
                even++;
            else
                odd++;
        }
        System.out.println("Even numbers: " + even);
        System.out.println("Odd numbers: " + odd);
    }
}

Bonus: Two-Dimensional Array Example

Code:


public class TwoDArray {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        System.out.println("Matrix elements:");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Conclusion

Mastering array programs in Java not only helps you crack interviews but also strengthens your logic-building skills. Arrays are the stepping stone to advanced data structures like linked lists, stacks, and queues.

Keep practicing these programs, understand the logic behind them, and try writing them without looking at the code. You can also explore variations such as rotating an array, merging two arrays, or finding the second largest element.

If you want a complete Java interview preparation guide, stay tuned to thewebdox.com — where we post coding tutorials, interview questions, and project-based learning resources.