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

java_array_exercises_with_solutions

Uploaded by

loltwitch1997
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
18 views2 pages

java_array_exercises_with_solutions

Uploaded by

loltwitch1997
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
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.

java.util.Arrays library provide many such util functions. There is another library that provides more advance utility functions for Array manipulations org.apache.commons.lang3.ArrayUtils. 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 =
Arrays.toString(intArray); // print directly will print reference value System.out.println(intArray); // [I@7150bd4d System.out.println(intArrayString); // [1, 2, 3, 4, 5] 2. Create ArrayList from array String[] stringArray = { "a", "b", "c", "d", "e" }; ArrayList arrayList = new ArrayList(Arrays.asList(stringArray)); System.out.println(arrayList); // [a, b, c,
d, e] 3.
Check if an array contains a certain value String[] stringArray = { "a", "b", "c", "d", "e" }; boolean b = Arrays.asList(stringArray).contains("a"); System.out.println(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 =
ArrayUtils.addAll(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 = StringUtils.join(new String[] { "a", "b", "c" }, ", "); System.out.println(j); // a, b, c 7.

Covnert ArrayList to Array String[] stringArray = { "a", "b", "c", "d", "e" }; ArrayList arrayList = new ArrayList(Arrays.asList(stringArray)); String[] stringArr = new String[arrayList.size()]; arrayList.toArray(stringArr); for (String s : stringArr) System.out.println(s); 8. Convert Array to Set Set set = new HashSet(Arrays.asList(stringArray));
System.out.println(set); //[d, e, b, c, a] 9. Reverse an array int[] intArray = { 1, 2, 3, 4, 5 }; ArrayUtils.reverse(intArray); System.out.println(Arrays.toString(intArray)); //[5, 4, 3, 2, 1] 10. Remove element of an array int[] intArray = { 1, 2, 3, 4, 5 }; int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed)); One more – convert int to byte array byte[] bytes = ByteBuffer.allocate(4).putInt(8).array(); for (byte t : bytes) { System.out.format("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--) { System.out.println(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++]; System.out.print(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 lines.Ans.for( int a=0; a < 7; a+ + ) { System.out.println( 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++) System.out.print(mat[x][y]+ " ");
System.out.println();Ans:13 6477 46Given the array a[ ]= {25, 58, 41, 32, 55, 30, 75, 80}; Answer the following questions.What 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[ ]Ans.The
indices of the array ar[ ] are 0-7Eight elements are in the arrayThe element at 3 index is 41.System.out.printin(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 array.Initialize a double dimensional array of 3 rows and 3 columns.Initialize 7 real or
decimal numbers in a single dimensional array.Ans.int 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 10.int m[ ]= {4, 6, 8, 12, 16, 3, 9, 7 };Ans.for( int j= 0; j<
m.length ; 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++) System.out.print(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 questions.How 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[ ]Ans.Seven elements are in the arrayThe indices of the array ar[ ] are 0-6The element at 4
index is 44,System.out.println(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]Ans.Element 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= x.length B= x[3]+x[1]*x[2]Ans.Write 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 nam.To print the string present at 4th position in string array nam[ ]To print character present at 3rd position in the string present at 2nd
index.To join the strings present at 2nd and 4th position.Ans.int y= nam . length;System.out.println( nam[ 4 ] );System.out.println(nam [2].charAt(3));System.out.println(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]; } System.out.print(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 program.Class First { public static void main(String args[]) { int sum[2,4]; for(i=0;i<2;i++) { for(j=0;j<=3;j++) { System.print(sum); } } } }Ans.class 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++) { System.out.print(sum[i][j]+“ ”); } } } } Identify error(s), if any, in the following program.class First { public static void main(String args[]) { int i; int a[6]={0,1,8,7,6,4}; for(i=0;i<=a.length();i++) System.out.println(a[i]); } }Ans.class 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 10.The easy approach for solving Array Manipulation is given below:We will solve Array Manipulation by finding the Maximum prefix array sum.Instead 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 3.In 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 problem.For: 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 200.So, 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 approach.Note: 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 index.long arrayManipulation(int n, vector> queries) {
vector A(n,0); // sum or difference of K's value in the vector long len = queries.size(); 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