Java Array Exercise
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
Click Here for the Solution
2. Java Program to Print a 2D Array.
Output:
[[ 1 , 2 , 3 ]
[ 4 , 5 , 6 ]
[ 7 , 8 , 9]]
Click Here for the Solution
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.
Click Here for the Solution
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.
Click Here for the Solution
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
Click Here for the Solution
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
Click Here for the Solution
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.
Click Here for the Solution
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).
Click Here for the Solution
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
Click Here for the Solution
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.
Click Here for the Solution
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 .
Click Here for the Solution
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.
Click Here for the Solution
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.
Click Here for the Solution
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.
Click Here for the Solution
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) )
Click Here for the Solution
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
Click Here for the Solution
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.
Click Here for the Solution
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 ]
Click Here for the Solution
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.
Click Here for the Solution
More Java Practice Exercises
To Practice Java Online please check our Practice Portal. <- Click Here
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!