Java Arrays Exercises
- Categories Java, Arrays in Java, Arrays, Arrays
- Give the total number of bytes occupied by these arrays?
- int ar[25]
- char M[3][4]
- double A[12]
Ans.
The number of bytes:
- ar[25] = 4*25 =100 bytes.
- M[3][4] = 2*3*4 =24 bytes
- A[12] = 8*12 =96 bytes
- What is the difference between these two statements:
- int sum[]=new int[10]
- sum[1]=10;
Ans.
- Declares an integer array of size 10 having the name ‘sum’.
- Initialises the second element of the array ‘sum’ with 10.
- Write the correct syntax of the following arrays:
- A1(2)
- Name[2,5]
- Roll[2;]
- Matrix(5)(5)
Ans.
- A1[2]
- Name[2][5]
- Roll[2]
- Matrix[5][5]
- Give the proper array declaration for the following:
- Declare an integer array, which can hold 25 values.
- Declare a two dimensional array called mat 3×4 of integer.
- Declare and initialise a two dimensional array of size 5×5 to 0.
Ans.
- int a[]=new int[25];
- int mat[][]=new int[3][4];
- int a[][]={{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
- Find the errors in the given program segment and re-write the statements correctly to assign values to an integer array.
int a= new int(5);
for(int i=0; i<=5; i++)
a[i]= I;
Ans.
The statement 1 has two errors: the square brackets „[ ]’ is required as “int[ ] a” or “int a[ ]”.
Also, the size of the array 5 should be within the „[ ]‟ bracket instead of ( ).
The statement 2 has one error. The loop should be 0 to 4 i.e. for( int i=0; i< 5; i++) or for( int
i= 0; i <5;i++)
Correct program code
int a[ ]= new int[ 5 ];
for(int i= 0; i <5; i++)
a[i]= i;
- 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.
1
2
3
4
5
6
- What 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 16
- If 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 64
77 46
- Given 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 indexes
- What is the start index and end index of array a[ ]
Ans.
- The indices of the array ar[ ] are 0-7
- Eight elements are in the array
- The element at 3 index is 41.
- System.out.printin(ar[2]+ “\t” +ar[6]);
- he start index is 0 and the end index is= 7
- Write 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 2
- Given 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 indexes
- What is the start index and end index of array ar[ ]
Ans.
- Seven elements are in the array
- The indices of the array ar[ ] are 0-6
- The element at 4 index is 44,
- System.out.println(ar[2]+ “\t” +ar[4]);
- The start index is 0 and the end index is= 6
- From the given array y[ ][ ]={{3, 4, 5},{6, 7, 8}.{1, 2, 3}}; answer the following
- Element at y[2][2]
- Element at y[0][1]
- Element at y[1][2]
Ans.
- Element at y[2][2]= 3
- Element at y[0][1]=4
- Element at y[1][2]= 8
- If 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.
- A=6
- B=66
- 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.
- If String nam[ ] = {“Can”, “Do”, “While”, “Text”, “Boy”, “Rat”, “Cat”);
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.
25
Adding all nos which are at even index
- Find 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<a.length;i++) System.out.println(a[i]); } }
- Find the errors in the given program segment and re-write the statements correctly to assign values to an integer array.
int a= new int(8); // statement 1
for(int i=0; i<=5; i++) // statement 2
a[i]= I;
Ans.
The statement 1 has two errors: the square brackets „[ ]’ is required as “int[ ] a” or “int a[ ]”.
Also, the size of the array 5 should be within the „[ ]‟ bracket instead of ( ).
The statement 2 has one error. The loop should be 0 to 4 i.e. for( int i=0; i< 5; i++) or for( int
i= 0; i <5;i++)
Correct program code
int a[ ]= new int[ 5 ];
for(int i= 0; i <5; i++)
a[i]= i;