50 Java Array Programs for Practice with Solutions

Arrays are one of the most important topics in Java programming. Whether you are preparing for college practical exams, coding interviews, semester assignments, or placement tests, array-based questions appear everywhere.

Many students understand Java syntax but struggle when they have to solve array problems independently. The best way to improve is through practice.

In this guide, you will find 50 Java array programs that help improve logical thinking, problem-solving skills, and interview preparation.


What Is an Array in Java?

An array is a collection of elements of the same data type stored under a single variable name.

Example:

int numbers[] = {10, 20, 30, 40, 50};

Instead of creating multiple variables, arrays allow you to store and manage multiple values efficiently.

Benefits of Arrays

  • Store multiple values using one variable
  • Easy data processing
  • Faster access through indexes
  • Widely used in data structures and algorithms

Basic Java Array Programs

These programs help beginners understand the fundamentals of arrays.

  1. Read and display array elements
  2. Find the sum of all elements
  3. Find the average of array elements
  4. Find the largest element
  5. Find the smallest element
  6. Count total elements in an array
  7. Display array elements in reverse order
  8. Copy one array into another
  9. Print even elements
  10. Print odd elements

Searching Programs in Arrays

Searching is one of the most important concepts in programming interviews.

  1. Linear Search
  2. Binary Search
  3. Search an element using user input
  4. Count occurrences of an element
  5. Find the first occurrence of an element
  6. Find the last occurrence of an element
  7. Search multiple elements
  8. Find a missing number in an array
  9. Check whether an element exists
  10. Find duplicate elements

Sorting Programs in Arrays

Sorting helps organize data efficiently.

  1. Sort array in ascending order
  2. Sort array in descending order
  3. Bubble Sort
  4. Selection Sort
  5. Insertion Sort
  6. Sort string arrays alphabetically
  7. Sort using Arrays.sort()
  8. Sort only even numbers
  9. Sort only odd numbers
  10. Sort without using built-in functions

Two-Dimensional Array Programs

Two-dimensional arrays are commonly used for matrices and tables.

  1. Add two matrices
  2. Subtract two matrices
  3. Multiply two matrices
  4. Find transpose of a matrix
  5. Calculate diagonal elements sum
  6. Display upper triangular matrix
  7. Display lower triangular matrix
  8. Check identity matrix
  9. Check symmetric matrix
  10. Check sparse matrix

Advanced Java Array Programs

Once you understand the basics, practice these advanced questions.

  1. Find the second largest element
  2. Find the second smallest element
  3. Remove duplicate elements
  4. Merge two arrays
  5. Split an array into two parts
  6. Rotate an array to the left
  7. Rotate an array to the right
  8. Find common elements between arrays
  9. Find a pair with a given sum
  10. Find the frequency of each element

Java Array Programs Most Asked in Interviews

If your goal is placement preparation or technical interviews, focus on these programs first:

  • Reverse an array
  • Largest element in an array
  • Smallest element in an array
  • Binary Search
  • Linear Search
  • Bubble Sort
  • Remove duplicates
  • Find missing numbers
  • Rotate arrays
  • Pair sum problem

These questions are commonly asked in fresher-level interviews and coding tests.


Example Program: Find Largest Element in an Array

public class LargestElement {
    public static void main(String[] args) {

        int arr[] = {12, 45, 78, 23, 90, 11};

        int largest = arr[0];

        for(int i = 1; i < arr.length; i++) {
            if(arr[i] > largest) {
                largest = arr[i];
            }
        }

        System.out.println("Largest Element = " + largest);
    }
}

Output

Largest Element = 90

Example Program: Reverse an Array

public class ReverseArray {
    public static void main(String[] args) {

        int arr[] = {10,20,30,40,50};

        for(int i = arr.length - 1; i >= 0; i--) {
            System.out.print(arr[i] + " ");
        }
    }
}

Output

50 40 30 20 10

Common Mistakes Students Make While Learning Arrays

Ignoring Array Indexes

Remember that array indexing starts from zero.

arr[0]

represents the first element.


Array Index Out of Bounds Exception

Accessing an invalid position causes:

ArrayIndexOutOfBoundsException

Always check array length before accessing elements.


Confusing Array Size and Element Count

Use:

arr.length

to find the total number of elements.


Why Should Beginners Practice Array Programs?

Array programming helps students:

  • Build programming logic
  • Improve problem-solving skills
  • Prepare for coding interviews
  • Understand data structures
  • Learn algorithms more easily

Arrays are the foundation for advanced topics such as:

  • Linked Lists
  • Stacks
  • Queues
  • Trees
  • Graphs

Without strong array concepts, advanced data structures become difficult.


Frequently Asked Questions

Are Java array programs important for interviews?

Yes. Arrays are among the most frequently asked topics in fresher interviews, internships, and coding assessments.

How many array programs should beginners practice?

Beginners should practice at least 20–30 array programs before moving to advanced data structures and algorithms.

Is Binary Search important for Java interviews?

Yes. Binary Search is a popular interview question because it tests logical thinking and algorithm understanding.

Which sorting algorithm should beginners learn first?

Bubble Sort and Selection Sort are the easiest sorting algorithms for beginners.


Final Thoughts

Arrays are one of the most important building blocks in Java programming. Mastering array programs improves coding confidence, problem-solving ability, and interview readiness.

Instead of memorizing solutions, focus on understanding the logic behind each program. Practice regularly, modify existing programs, and gradually move toward advanced data structures.

The more array problems you solve, the stronger your Java programming skills will become.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.