0% found this document useful (0 votes)
152 views2 pages

Java Array Exercises With Solutions

Uploaded by

loltwitch1997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views2 pages

Java Array Exercises With Solutions

Uploaded by

loltwitch1997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java array exercises with solutions pdf

Arrays in Java are a powerful way to store multiple elements of Similar type. You can read more about them here (Arrays in Java). You can also refer to the official Oracle documentation for more information. Since the advancement in Java version there are many utility functions that has made the manipulations and working with Arrays simpler.

[Link] library provide many such util functions. There is another library that provides more advance utility functions for Array manipulations [Link]. Below are the beginner as well as advanced level practice questions that involves Java Arrays concept. Although there are solutions given next to each of the
question, we recommend that you come up with your own solution and only check the our solution to compare the approach. Even though many of the below questions can easily be solved by using utility library methods, it would be better to approach with your own solution to understand how arrays are implemented internally. Good Luck ! Exercise
Questions The following are top 10 methods for Java Array. They are the most voted questions from stackoverflow. 0. Decalre an array String[] aArray = new String[5]; String[] bArray = {"a","b","c", "d", "e"}; String[] cArray = new String[]{"a","b","c","d","e"}; 1. Print an array in Java int[] intArray = { 1, 2, 3, 4, 5 }; String intArrayString =
[Link](intArray); // print directly will print reference value [Link](intArray); // [I@7150bd4d [Link](intArrayString); // [1, 2, 3, 4, 5] 2. Create ArrayList from array String[] stringArray = { "a", "b", "c", "d", "e" }; ArrayList arrayList = new ArrayList([Link](stringArray)); [Link](arrayList); // [a, b, c,
d, e] 3.
Check if an array contains a certain value String[] stringArray = { "a", "b", "c", "d", "e" }; boolean b = [Link](stringArray).contains("a"); [Link](b); // true 4. Concatenate two arrays int[] intArray = { 1, 2, 3, 4, 5 }; int[] intArray2 = { 6, 7, 8, 9, 10 }; // Apache Commons Lang library int[] combinedIntArray =
[Link](intArray, intArray2); 5. Declare array inline method(new String[]{"a", "b", "c", "d", "e"}); 6. Joins the elements of the provided array into a single String // containing the provided list of elements // Apache common lang String j = [Link](new String[] { "a", "b", "c" }, ", "); [Link](j); // a, b, c 7.

Covnert ArrayList to Array String[] stringArray = { "a", "b", "c", "d", "e" }; ArrayList arrayList = new ArrayList([Link](stringArray)); String[] stringArr = new String[[Link]()]; [Link](stringArr); for (String s : stringArr) [Link](s); 8. Convert Array to Set Set set = new HashSet([Link](stringArray));
[Link](set); //[d, e, b, c, a] 9. Reverse an array int[] intArray = { 1, 2, 3, 4, 5 }; [Link](intArray); [Link]([Link](intArray)); //[5, 4, 3, 2, 1] 10. Remove element of an array int[] intArray = { 1, 2, 3, 4, 5 }; int[] removed = [Link](intArray, 3);//create a new array
[Link]([Link](removed)); One more – convert int to byte array byte[] bytes = [Link](4).putInt(8).array(); for (byte t : bytes) { [Link]("0x%x ", t); } In addition, do you know what arrays look like in memory ? What will be the output of the following program:class Output { public static void main(String
args[]) { int a[]={6,5,4,3,2,1}; int x; for(x=5;x>=0;x--) { [Link](a[x]); } } }Ans.123456What will be the output of the following program:class First { public static void main(String args[]) { int a[]={5,1,15,20,25}; int i,j; int m; i=++a[1]; j=a[2]++; m=a[i++]; [Link](i+“ ”+j+“ ”+m); } } Ans.3 15 16If String nam[ ]={ “simply”,
“coding”, “While”, “programs”, “Boy”, “java”, “Cat”} ;.Write java for loop to print all the strings into different [Link]( int a=0; a < 7; a+ + ) { [Link]( nam[ a ]); }What is the output of this code:int mat[ ][ ]= {{13,64},{77,46}}; for(int x=0;x<2;x++){ for(int y= 0; y<2; y++) [Link](mat[x][y]+ " ");
[Link]();Ans:13 6477 46Given the array a[ ]= {25, 58, 41, 32, 55, 30, 75, 80}; Answer the following [Link] are the indices of the array a[ ]?How many elements are in the array?What is the 3th element of array?Write the statement to print elements of 2nd and 6th indexesWhat is the start index and end index of array a[ ][Link]
indices of the array ar[ ] are 0-7Eight elements are in the arrayThe element at 3 index is [Link](ar[2]+ “\t” +ar[6]);he start index is 0 and the end index is= 7Write valid Java statements for the following:Initialize 10 integers in a single dimensional [Link] a double dimensional array of 3 rows and 3 [Link] 7 real or
decimal numbers in a single dimensional [Link] y[ ]= {10, 5, 6, 12, 7, 8,9, 4, 24, 88};int num[ ][ ]= {1, 2, 3}, {7, 8, 9}, {4, 5, 6}};double x[ ]= {2.0, 4.5, 6.8, 3.0, 1.1, 7.5, 9.0};Consider the given array and write a valid Java program code to update each element of the array by adding [Link] m[ ]= {4, 6, 8, 12, 16, 3, 9, 7 };[Link]( int j= 0; j<
[Link] ; j++) { m[j]= m[j]+10; }What is the output of this code segment?int G[][] = { {4, 3, 6}, {3, 4, 9}, {3, 7, 2} }; int c=0; int arr[ ]= new int[ 9 ]; for( int j= 0; j<3; j++){ for( int h=0; h <3 ; h++){ arr[c]= G[ j ][ h ]; c++; } } for (int j = 0; j <9 ; j++) [Link](arr[j] + “ “); Ans: 4 3 6 3 4 9 3 7 2Given the array ar[ ]= {10, 22, 33, 44, 55.
66, 77}; Answer the following [Link] many elements are in the array?What are the indices of the array ar[ ]?What is the 4th element of array?Write the statement to print elements of 2nd and 4th indexesWhat is the start index and end index of array ar[ ][Link] elements are in the arrayThe indices of the array ar[ ] are 0-6The element at 4
index is 44,[Link](ar[2]+ “\t” +ar[4]);The start index is 0 and the end index is= 6From the given array y[ ][ ]={{3, 4, 5},{6, 7, 8}.{1, 2, 3}}; answer the followingElement at y[2][2]Element at y[0][1]Element at y[1][2][Link] at y[2][2]= 3Element at y[0][1]=4Element at y[1][2]= 8If int x[]= {5,9,7,3,2,8}; What are the value of A and
B? A= [Link] B= x[3]+x[1]*x[2][Link] single line statement to implement the following:If String nam[ ] = {“Can”, “Do”, “While”, “Text”, “Boy”, “Rat”, “Cat”);Count the number of names in the data structure [Link] print the string present at 4th position in string array nam[ ]To print character present at 3rd position in the string present at 2nd
[Link] join the strings present at 2nd and 4th [Link] y= nam . length;[Link]( nam[ 4 ] );[Link](nam [2].charAt(3));[Link](nam [2].concat(nam[4]));What will be the output of the following program:class Output { public static void main(String args[]) { int a,b=0; int c[]={1,2,3,4,5,6,7,8,9,10};
for(a=0;a<10;a++) { if(a%2==0) b+=c[a]; } [Link](b); } }In what statement, what is the output?

State what the above program is doing?Ans.25Adding all nos which are at even indexFind the syntax error(s), if any in the following [Link] First { public static void main(String args[]) { int sum[2,4]; for(i=0;i<2;i++) { for(j=0;j<=3;j++) { [Link](sum); } } } }[Link] First { public static void main(String args[]) { int sum[][]=new
int[2][4]; for(i=0;i<2;i++) { for(j=0;j<=3;j++) { [Link](sum[i][j]+“ ”); } } } } Identify error(s), if any, in the following [Link] First { public static void main(String args[]) { int i; int a[6]={0,1,8,7,6,4}; for(i=0;i<=[Link]();i++) [Link](a[i]); } }[Link] First { public static void main(String args[]) { int i; int a[]=
{0,1,8,7,6,4}; for(i=0;i 1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 0 0 0 0 0 3 3 3 10 10 7 7 7 0 0 3 3 3 10 10 8 8 8 1 0 The largest value is 10 after all operations. So the answer will be [Link] easy approach for solving Array Manipulation is given below:We will solve Array Manipulation by finding the Maximum prefix array [Link] of using
two loops, we will apply this technique:A[Start_Index] = A[Start_Index] + k A[End_index+1] = A[End_index+1] – k Understand this by an example:5 3 1 2 100 2 5 100 3 4 100Array is of size 5.

Number of operations is [Link] Image, we can see the first operation for 1 2 100. Using the same method for the remaining two operations we get1 2 100 | 100 0 -100 0 0 2 5 100 | 100 100 -100 0 0 3 4 100 | 100 100 0 0 -100If you want to understand the logic then please
make the same table for the uppermost [Link]: 2 5 100 – A[5+1] = A[6] which is a segmentation fault to remove this we will use one condition described in the code below. 3. At last, we will calculate the max prefix sum of the modified array which will be [Link], here is the main logic for Array Manipulation in C++. Please Dry and run the
code with one of the given examples above. It will help you to understand the logic behind this [Link]: For a better understanding I have used 1 as a first index in the algorithm. ffxiv foxy lady fate timer In programming, you will see some minute changes as it is solved by taking 0 as a first [Link] arrayManipulation(int n, vector> queries) {
vector A(n,0); // sum or difference of K's value in the vector long len = [Link](); for(long i=0; isum) { sum = x; } } return sum; } Forming a Magic Square : HackeRank Solution in C++Day of the Programmer in C++ : HackerRank SolutionHackerRank Solution : Divisible Sum Pairs in C++HackerRank Solution : Birthday Chocolate in C++Hacker
Rank Solution in C++ : Dynamic ArrayHacker Rank Problem : 2D Array DS SolutionHackerRank Solution : Breaking the Records in C++

You might also like