Arrays are one of the most fundamental data structures in Java. They allow you to store multiple values of the same data type in a single variable instead of creating separate variables for each value.
Without arrays:
int mark1 = 80;
int mark2 = 85;
int mark3 = 90;
int mark4 = 75;
int mark5 = 95;
Using an array:
int[] marks = {80, 85, 90, 75, 95};
Arrays make programs:
- More organized
- Easier to manage
- Faster to process
- Suitable for large datasets
Arrays are widely used in:
- Student Management Systems
- Banking Applications
- Employee Management Systems
- Inventory Software
- Data Analytics
- Android Development
- Enterprise Applications
- Game Development
- Competitive Programming
In Java, every array has:
- Fixed Size
- Index-Based Access (starts from index 0)
- Same Data Type Elements
- Contiguous Memory Allocation
In this chapter, you’ll solve beginner-friendly and interview-oriented Java array practice questions. Each question includes a complete Java solution, sample input/output, explanation, and concepts covered to strengthen your understanding of arrays. Java Arrays practice questions with solutions help to build concepts.
1. Java Program to Read and Print Array Elements
Problem Statement
Write a Java program to accept 5 integer values from the user and display all array elements.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
System.out.println("\nArray Elements:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
scanner.close();
}
}
Sample Input
10
20
30
40
50
Sample Output
Array Elements:
10
20
30
40
50
Explanation
The first loop stores values inside the array.
The second loop prints every element stored in the array.
Concepts Covered
- Array Declaration
- Array Input
- Array Output
- for Loop
2. Java Program to Find the Sum of Array Elements
Problem Statement
Write a Java program to calculate the sum of all elements present in an array.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
int sum = 0;
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
sum += numbers[i];
}
System.out.println("Sum = " + sum);
scanner.close();
}
}
Sample Input
5
10
15
20
25
Sample Output
Sum = 75
Explanation
Each array element is added to the sum variable during input.
The final value represents the total of all array elements.
Concepts Covered
- Arrays
- Accumulator Variable
- for Loop
- Arithmetic Operations
3. Java Program to Find the Largest Element in an Array
Problem Statement
Write a Java program to find the largest element in an array.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
int largest = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
System.out.println("Largest Element = " + largest);
scanner.close();
}
}
Sample Input
25
18
95
42
63
Sample Output
Largest Element = 95
Explanation
The first element is assumed to be the largest.
The program compares every remaining element and updates the largest variable whenever a bigger value is found.
Concepts Covered
- Arrays
- if Statement
- Largest Element
- Loop Traversal
4. Java Program to Find the Smallest Element in an Array
Problem Statement
Write a Java program to find the smallest element present in an array.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
int smallest = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}
System.out.println("Smallest Element = " + smallest);
scanner.close();
}
}
Sample Input
45
22
87
11
60
Sample Output
Smallest Element = 11
Explanation
The first array element is assumed to be the smallest.
The program compares each remaining element with the current smallest value and updates it whenever a smaller value is found.
Concepts Covered
- Arrays
- if Statement
- Smallest Element
- Loop Traversal
5. Java Program to Calculate the Average of Array Elements
Problem Statement
Write a Java program to calculate the average of all elements stored in an array.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
int sum = 0;
double average;
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
sum += numbers[i];
}
average = (double) sum / numbers.length;
System.out.println("Average = " + average);
scanner.close();
}
}
Sample Input
20
40
60
80
100
Sample Output
Average = 60.0
Explanation
The program first calculates the total sum of all array elements.
Then it divides the sum by the total number of elements (numbers.length) to calculate the average.
The result is stored in a double variable to preserve decimal values.
Concepts Covered
- Arrays
- Sum of Array Elements
- Average Calculation
- Type Casting
- Array Length Property
6. Java Program to Reverse an Array
Problem Statement
Write a Java program to print the elements of an array in reverse order.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
System.out.println("\nArray in Reverse Order:");
for (int i = numbers.length - 1; i >= 0; i--) {
System.out.println(numbers[i]);
}
scanner.close();
}
}
Sample Input
10
20
30
40
50
Sample Output
50
40
30
20
10
Explanation
The loop starts from the last index (length - 1) and moves backward until the first element.
This prints the array in reverse order without modifying the original array.
Concepts Covered
- Arrays
- Reverse Traversal
- for Loop
- Array Length Property
7. Java Program to Copy One Array into Another
Problem Statement
Write a Java program to copy all elements of one array into another array.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] source = new int[5];
int[] destination = new int[5];
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < source.length; i++) {
source[i] = scanner.nextInt();
}
for (int i = 0; i < source.length; i++) {
destination[i] = source[i];
}
System.out.println("\nCopied Array:");
for (int i = 0; i < destination.length; i++) {
System.out.println(destination[i]);
}
scanner.close();
}
}
Sample Input
11
22
33
44
55
Sample Output
11
22
33
44
55
Explanation
The program copies every element from the source array to the destination array using a loop.
Concepts Covered
- Arrays
- Copying Arrays
- Loop Traversal
8. Java Program to Search an Element in an Array
Problem Statement
Write a Java program to search for a specific element in an array.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
int search;
boolean found = false;
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
System.out.print("Enter Number to Search: ");
search = scanner.nextInt();
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == search) {
found = true;
break;
}
}
if (found) {
System.out.println("Element Found.");
} else {
System.out.println("Element Not Found.");
}
scanner.close();
}
}
Sample Input
5
10
15
20
25
15
Sample Output
Element Found.
Explanation
The program compares the search value with every array element.
If a match is found, the loop stops immediately using the break statement.
Concepts Covered
- Arrays
- Linear Search
- Boolean Variable
- break Statement
9. Java Program to Count Even and Odd Numbers in an Array
Problem Statement
Write a Java program to count how many even and odd numbers are present in an array.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
int even = 0;
int odd = 0;
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 == 0) {
even++;
} else {
odd++;
}
}
System.out.println("Even Numbers = " + even);
System.out.println("Odd Numbers = " + odd);
scanner.close();
}
}
Sample Input
2
5
8
11
20
Sample Output
Even Numbers = 3
Odd Numbers = 2
Explanation
Each array element is checked using the modulus operator.
- Remainder = 0 → Even
- Otherwise → Odd
The program maintains separate counters for even and odd numbers.
Concepts Covered
- Arrays
- Even Numbers
- Odd Numbers
- Modulus Operator
10. Java Program to Find the Second Largest Element in an Array
Problem Statement
Write a Java program to find the second largest element in an array.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int number : numbers) {
if (number > largest) {
secondLargest = largest;
largest = number;
} else if (number > secondLargest && number != largest) {
secondLargest = number;
}
}
System.out.println("Second Largest = " + secondLargest);
scanner.close();
}
}
Sample Input
20
95
45
60
80
Sample Output
Second Largest = 80
Explanation
The program keeps track of:
- Largest element
- Second largest element
Whenever a new largest value is found, the previous largest becomes the second largest.
Concepts Covered
- Arrays
- Largest Element
- Second Largest Element
- Enhanced for Loop
- Comparison Logic
11. Java Program to Sort an Array in Ascending Order
Problem Statement
Write a Java program to sort the elements of an array in ascending order.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[i] > numbers[j]) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
System.out.println("\nAscending Order:");
for (int number : numbers) {
System.out.print(number + " ");
}
scanner.close();
}
}
Sample Input
45
12
78
23
56
Sample Output
Ascending Order:
12 23 45 56 78
Explanation
The program compares each array element with the remaining elements. If a smaller value is found, the two elements are swapped.
This process continues until the array is sorted in ascending order.
Concepts Covered
- Nested Loops
- Arrays
- Swapping
- Sorting Logic
12. Java Program to Sort an Array in Descending Order
Problem Statement
Write a Java program to sort the elements of an array in descending order.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
System.out.println("Enter 5 Numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[i] < numbers[j]) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
System.out.println("\nDescending Order:");
for (int number : numbers) {
System.out.print(number + " ");
}
scanner.close();
}
}
Sample Input
20
60
10
90
40
Sample Output
Descending Order:
90 60 40 20 10
Explanation
The program swaps elements whenever a larger value is found, producing a descending order array.
Concepts Covered
- Arrays
- Sorting
- Nested Loops
- Swapping
13. Java Program to Merge Two Arrays
Problem Statement
Write a Java program to merge two arrays into a single array.
Java Solution
public class Main {
public static void main(String[] args) {
int[] firstArray = {10, 20, 30};
int[] secondArray = {40, 50, 60};
int[] mergedArray = new int[firstArray.length + secondArray.length];
int index = 0;
for (int number : firstArray) {
mergedArray[index++] = number;
}
for (int number : secondArray) {
mergedArray[index++] = number;
}
System.out.println("Merged Array:");
for (int number : mergedArray) {
System.out.print(number + " ");
}
}
}
Sample Output
Merged Array:
10 20 30 40 50 60
Explanation
The program first copies the elements of the first array, then appends all elements of the second array.
Concepts Covered
- Arrays
- Enhanced for Loop
- Merging Arrays
14. Java Program to Remove Duplicate Elements from an Array
Problem Statement
Write a Java program to display only unique elements from an array.
Java Solution
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 10, 30, 20};
System.out.println("Unique Elements:");
for (int i = 0; i < numbers.length; i++) {
boolean duplicate = false;
for (int j = 0; j < i; j++) {
if (numbers[i] == numbers[j]) {
duplicate = true;
break;
}
}
if (!duplicate) {
System.out.print(numbers[i] + " ");
}
}
}
}
Sample Output
Unique Elements:
10 20 30
Explanation
Each element is compared with all previously visited elements.
If it already exists, it is skipped; otherwise, it is printed.
Concepts Covered
- Arrays
- Nested Loops
- Duplicate Detection
- Boolean Variables
15. Java Program to Find the Frequency of Each Element in an Array
Problem Statement
Write a Java program to calculate the frequency of each element in an array.
Java Solution
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 10, 30, 20};
boolean[] visited = new boolean[numbers.length];
for (int i = 0; i < numbers.length; i++) {
if (visited[i]) {
continue;
}
int count = 1;
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[i] == numbers[j]) {
count++;
visited[j] = true;
}
}
System.out.println(numbers[i] + " occurs " + count + " time(s)");
}
}
}
Sample Output
10 occurs 2 time(s)
20 occurs 2 time(s)
30 occurs 1 time(s)
Explanation
The program counts how many times each element appears in the array while avoiding duplicate counting using a visited array.
Concepts Covered
- Arrays
- Frequency Counting
- Nested Loops
- Boolean Array
Chapter Summary
In this chapter, you learned the fundamentals of Java Arrays, one of the most important data structures in Java programming. Arrays allow you to store multiple values of the same data type in a single variable, making your code more organized, efficient, and easier to maintain.
Throughout this chapter, you solved practical Java array programs such as:
- Reading and printing array elements
- Calculating the sum and average of array elements
- Finding the largest and smallest element
- Reversing an array
- Copying one array into another
- Searching for an element
- Counting even and odd numbers
- Finding the second largest element
- Sorting arrays in ascending and descending order
- Merging arrays
- Removing duplicate elements
- Finding the frequency of array elements
These programs are commonly asked in Java coding interviews and form the foundation for advanced topics like ArrayLists, Collections Framework, Searching Algorithms, Sorting Algorithms, and Data Structures.
By mastering arrays, you’ll be well prepared for more advanced Java concepts and real-world application development.
Key Takeaways
- Arrays store multiple values of the same data type.
- Array indexing starts from 0.
- The size of an array is fixed after creation.
- Arrays are accessed using indexes.
- The
lengthproperty returns the size of an array. - Loops are commonly used to traverse arrays.
- Searching and sorting are among the most important array operations.
- Nested loops are frequently used for sorting and duplicate removal.
- Arrays are the foundation for many advanced Java data structures.
- Strong array knowledge is essential for coding interviews and competitive programming.
Frequently Asked Questions (FAQs)
1. What is an array in Java?
An array is a collection of elements of the same data type stored in contiguous memory locations. It allows multiple values to be managed using a single variable.
2. How do you declare an array in Java?
Example:
int[] numbers = new int[5];
This creates an integer array capable of storing five elements.
3. What is the default index of the first array element?
The first element of every Java array is stored at index 0.
Example:
numbers[0]
4. How can we find the size of an array?
Use the length property.
Example:
System.out.println(numbers.length);
5. Can we change the size of an array after creation?
No.
The size of a Java array is fixed. If you need a dynamic-size collection, use ArrayList.
6. Which loop is best for traversing arrays?
The for loop and enhanced for-each loop are most commonly used.
Example:
for(int number : numbers)
{
System.out.println(number);
}
7. What is the difference between an array and an ArrayList?
| Array | ArrayList |
|---|---|
| Fixed size | Dynamic size |
| Faster | Slightly slower |
| Stores primitive values directly | Stores objects |
| Part of Java language | Part of Java Collections Framework |
8. Why are arrays important in Java?
Arrays are used in:
- Searching Algorithms
- Sorting Algorithms
- Data Structures
- Game Development
- Android Development
- Banking Software
- Student Management Systems
- Competitive Programming
- Enterprise Applications
They are one of the most frequently asked topics in Java interviews.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
