An array is a group of elements with similar data types that are bound together as one. The array allows us to store and manipulate a collection of elements together. Mastering arrays is essential for any Java developer, as it provides a solid foundation for more complex data structures and algorithms. In this article, we will learn about Java Array with Java Practice Problems.
In this practice blog, we will dive into Java Array exercises to help you strengthen your Array skills. It is both beginner and experienced-friendly. So, if you are ready to tackle some Java array practice problems and take your coding skills to the next level, let's get started!
Basic Array Questions
As we said above, Array is one of the most important and fundamental data structures in the Java programming language. So, in this section, we have covered all the basic Array-based Java exercises.

1. Java Program to Add Elements in an Array.
Input: Array: [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
Element: 50
Output: Array: [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 50 ]
Explanation: Adding Element in the Array
2. Java Program to Print a 2D Array.
Output:
[[ 1 , 2 , 3 ]
[ 4 , 5 , 6 ]
[ 7 , 8 , 9]]
3. Array Program to Add Two Matrices
Input:
A[][] = [[ 1 , 2 ],
[ 3 , 4 ]]
B[][] = [[ 1 , 1 ],
[ 1 , 1 ]]
Output:
Result = [[ 2 , 3 ]
[ 4 , 5 ]]
Explanation: Adding Elements of both the Matrices and Printing the Result.
4. Java Array Program to Find the Transpose
Input:
[ [ 1, 2, 3 ]
[ 4, 5, 6 ]
[ 7, 8, 9 ] ]
Output:
[ [ 1, 4, 7]
[ 2, 5, 8]
[ 3, 6, 9] ]
Explanation: Transpose of a matrix is obtained by changing rows to columns and columns to rows.
5. Java Array Program to Find the Determinant
Input: [ 1 , -3 , -2 ]
[ -1 , 2 , 1 ]
[ 1 , 0 , -2 ]
Output: 3
Explanation: 1*(-4-0) + 3*(2-1) + (-2)*(0-2) = 3
6. Java Array Program For Array Rotation
Input: arr[] = {1, 2, 3, 4, 5, 6, 7} , d = 3
Output:Â 4 5 6 7 1 2 3
Explanation: Taking the element from the beginning from the front and adding it to the end of the array
Intermediate Array Questions
Expect challenges that involve manipulating, searching, and transforming arrays in more complex ways. This section will solidify your understanding and prepare you for advanced array concepts.
7. Java Array Program to Rotate Matrix Elements
Input:
[ 1 , 2 , 3 ]
[ 4 , 5 , 6 ]
[ 7 , 8 , 9 ]
Output:
[ 4 , 1 , 2 ]
[ 7 , 5 , 3 ]
[ 8 , 9 , 6 ]
Explanation: We need to shift all the elements in Clockwise in the Matrix.
8. Java Array Program to Compute the Sum of Diagonals of a Matrix
Input:
[ 1 , 2 , -3 ]
[ 4 , 5 , 6 ]
[ 7 , 8 , -9 ]
Output: Primary Diagonal: 9
Secondary Diagonal: -3
Explanation: Out of Two diagonal the higher sum is called primary diagonal that is (-3+5+7) and second one is called secondary diagonal(1+5-9).
9. Java Array Program to Print Boundary Elements of a Matrix
Input :
[ 1 , 2 , 3 , 4]
[ 5 , 6 , 7 , 8 ]
[ 9 , 10 , 11 , 12 ]
[ 13 , 14 , 15 , 16 ]
Output:
1 2 3 4
5 8
9 12
13 14 15 16
10. Java Array Program to Remove Duplicate Elements From an Array
Input:Â arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}
Output:Â arr[] = {1, 2, 3, 4, 5}
Explanation: All the elements in the array should be unique there should not be any duplicate element in array.
11. Java Array Program to Remove All Occurrences of an Element in an Array
Input: array = { 10, 20, 10, 30, 50, 10 }, key = 10
Output: [20, 30, 50]
Explanation: Removing element key(10) all occurrences from the array and then returning the array .
12. Java Array Program to Merge Two Arrays
Input: arr1[] = [ 5, 8, 9 ]
arr2[] = [ 4, 7, 10 ]
Output: result[] = [ 5, 8, 9, 4, 7, 10 ]
Explanation: Returning the array result which contains elements from arr1 and then elements from arr2 added in it.
13. Java Array Program to Sort the 2D Array Across Columns
Input: arr = [ [ 39, 27, 11, 42 ],
[ 10, 93, 91, 90 ],
[ 54, 78, 56, 89 ],
[ 24, 64, 20, 65 ] ];
col = 3
Output: [ [ 39 , 27 , 11 , 42 ],
[ 24 , 64 , 20 , 65 ],
[ 54 , 78 , 56 , 89 ],
[ 10 , 93 , 91 , 90 ] ];
Explanation:
Sorting the array according the col(3). So, all the rows are placed in such a way that column is sorted.
14. Java Array Program to Sort the Elements of an Array in Descending Order
Input : Arr = {2, 6, 23, 98, 24, 35, 78}
Output : [98, 78, 35, 24, 23, 6, 2]
Explanation: All the elements are sorted in Descending order.
15. Java Program for Sorting an Array using Merge Sort.
Input: [ 12 , 11 , 13 , 5 , 6 , 7 ]
Output: [ 5 , 6 , 7 , 11 , 12 , 13 ]
Explanation: Using Merge Sort to Sort the array. Time Complexity: O( N log(N) )
Additional Questions DSA
Have you got the DSA basics down? So get ready to dive deeper! Explore advanced concepts, troubleshooting techniques, or even interview questions to solidify your DSA knowledge and prepare for coding challenges.
1. Program Print Matrix in Spiral Form.
Input:Â Â [[ 1, Â Â 2, Â 3, Â 4 ],
       [ 5,   6,  7,  8 ],
       [ 9,  10,  11,  12 ],
      [ 13,  14,  15,  16 ]]
Output:Â 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10Â
2. Program to Set Entire Matrix Row and Column as Zeroes
Input:Â [ [ 1 , 1 , 1 ],
[ 1 , 0 , 1 ],
[ 1 , 1 , 1 ]]
Output:Â [ [1, 0, 1],
[0, 0, 0],
[1, 0, 1]]
Explanation: All the elements with the direct contact with 0 turns into 0.
3. Program to Sort an array of 0s, 1s and 2s.
Input: [ 0 , 1 , 2 , 0 , 1 , 2]
Output: [ 0 , 0 , 1 , 1 , 2 , 2 ]
4. Program to Move all Zeroes to end of Array
Input : arr[] = {1, 2, 0, 0, 0, 3, 6};
Output : arr[] = {1, 2, 3, 6, 0, 0, 0};
Explanation: Rearrange all the elements in such way that the sequence is not changed and zero.
More Java Practice Exercises
Conclusion
That's it for our Java Array exercise crash course! The exercises throughout this guide are your playground to experiment and solidify your skills in Java Array. Remember, practice makes perfect when it comes to arrays (and coding in general!). So bookmark this guide, revisit the exercises, and keep building awesome Java projects!
Similar Reads
Java Features
Java is a high-level, object-oriented programming language. It is known for its platform independence, reliability, and security. Java Programming language follows the "Write Once, Run Anywhere" principle. It provides various features like portability, robustness, simplicity, multithreading, and hig
7 min read
Interesting Facts About Java
Java: A general-purpose, high-level programming language. It is developed by Sun Microsystems. It was developed by a mini team of engineers which is known as the Green Team. They initiated this language in 1991. Here are some interesting facts about Java: The initial name of java was "Oak". It was c
2 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Core Java Interview Questions For Freshers
For the latest Java Interview Questions Refer to the Following Article â Java Interview Questions â Fresher and Experienced (2025) Java is one of the most popular and widely used programming languages and a platform that was developed by James Gosling in the year 1995. It is based on the concept of
15 min read
Learn Java - A Beginners Guide for 2024
If you are new to the world of coding and want to start your coding journey with Java, then this learn Java a beginners guide gives you a complete overview of how to start Java programming. Java is among the most popular and widely used programming languages and platforms. A platform is an environme
10 min read
Build a Calculate Expression Game in Java
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applic
14 min read
5 Tips to Get a Job as a Java Fresher
Java is one of the most robust programming languages, which is currently used for development in 3 billion devices. This language offers amazing features like platform independence, object-oriented programming, enhanced security features, automatic garbage collection, and many more. The latest techn
6 min read
Classes and Objects in Java
In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh
12 min read
Implement Interface using Abstract Class in Java
Interface contains only abstract methods that can't be instantiated and it is declared by keyword interface. A class that is declared with the abstract keyword is known as an abstract class in Java. This is a class that usually contains at least one abstract method which can't be instantiated and It
3 min read
Introduction to Java
Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is intended to let application developers Write Once and Run Anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the
9 min read