# Array Test Questions for Java/BlueJ

[Set I]
Time: 1 Hrs                                                                            Marks: 5x5=25
1. Write a program to generate a Pascal Triangle in which each number in the row is the sum of the two numbers immediately above it. The program should print the first 5 rows of the triangle.
public class ques39
{
public static void main()
{
int size=5;
int a[][]=new int[size][size];
int i,j;
a[0][0]=1;
// prepare the pascal triangle //
for(i=1;i<size;i++)
{
for(j=0;j<size;j++)
{
if((i==j) || (j==0))
a[i][j]=1;
else
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
// display the pascal triangle //
for(i=0;i<size;i++)
{
for(j=1;j<=15-i;j++)
System.out.print (" ");
for(j=0;j<=i;j++)
System.out.print (a[i][j]+" ");
System.out.println ();
}
}
}
2. Assign numbers into an array of size 4X4. Then calculate the sum of numbers present in the diagonals position, also calculate the sum of each row total and column total.
public class RowColDiagTotal
{
public static void main()
{
int a[][]={{4,5,8,1},
{2,8,7,4},
{3,4,5,6},
{1,2,7,8}};
int i,j,Row=0, Col=0, Diag1=0,Diag2=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j)
Diag1+=a[i][j];
if((i+j)==3)
Diag2+=a[i][j];
}
}
System.out.println ("Sum of Left diagonals :"+Diag1);
System.out.println ("Sum of RIght diagonals :"+Diag2);
for(i=0;i<4;i++)
{
Row=0;
for(j=0;j<4;j++)
{
Row+=a[i][j];
}
System.out.println ("Row "+(i+1)+" total is : "+Row);
}
for(i=0;i<4;i++)
{
Col=0;
for(j=0;j<4;j++)
{
Col+=a[j][i];
}
System.out.println ("Column "+(i+1)+" total is : "+Col);
}
}
}
3. Assign 10 numbers into an array and then find the Highest and Lowest value in the array and also find their position in array.
public class MaxMinPos
{
public static void main()
{
int i;
int a[]={3,7,2,4,1,6,7,10,9,8};
int max=a[0],min=a[0];
int maxPos=0,minPos=0;
for(i=0;i<10;i++)
{
if(a[i]>max)
{
max=a[i];
maxPos=i;
}
if(a[i]<min)
{
min=a[i];
minPos=i;
}
}
System.out.println ("Maximum No. = "+max);
System.out.println ("Position = "+maxPos);
System.out.println ("Minimum No. = "+min);
System.out.println ("Position = "+minPos);
}
}
4. Accept the name and total marks of 15 students from user. Then display the list according to their merit. (Use Bubble sort technique)
import java.io.DataInputStream;
import java.io.IOException;
public class ques19
{
public static void main() throws IOException
{
DataInputStream stdin = new DataInputStream(System.in);
String n[]=new String[5];
int m[]=new int[5];
int i,j,k;
for(i=0;i<5;i++)
{
System.out.print ("Enter Name : ");
System.out.print ("Enter Comp. Sc. Marks : ");
}
// Sort the array in Descending order
String x=null;
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(m[i]<m[j])
{
k=m[i]; m[i]=m[j]; m[j]=k;
x=n[i]; n[i]=n[j]; n[j]=x;
}
}
}
// display Merit List
System.out.println ("Name \t\t Marks");
for(i=0;i<5;i++)
System.out.println (n[i]+"\t\t"+m[i]);
} // end of method
}
5. WAP in Java to accept names of the 10 politicians and their party to which they belong to in two separate arrays. Ask the users to enter the name of the party. Printout all the names of the politicians who belong to that party.
import java.io.*;
public class ques23
{
public static void main() throws IOException
{
DataInputStream stdin = new DataInputStream(System.in);
String party[]=new String[10];
String poli[]=new String[10];
String search;
boolean flag=false;
for(int i=0;i<10;i++)
{
System.out.print ("Enter Party Name : ");
System.out.print ("Enter Politician Name : ");
}
System.out.print ("Enter Searching Party Name : ");
System.out.println ("Name of Politician's");
for(int i=0;i<5;i++)
if(search.compareTo(party[i])==0)
System.out.println (poli[i]);
}
}
[Set II]
Time: 1 Hrs                                                                            Marks: 5x5=25
1. Write a program to generate a Pascal Triangle in which each number in the row is the sum of the two numbers immediately above it. The program should print the first 5 rows of the triangle.
public class ques39
{
public static void main()
{
int size=5;
int a[][]=new int[size][size];
int i,j;
a[0][0]=1;
// prepare the pascal triangle //
for(i=1;i<size;i++)
{
for(j=0;j<size;j++)
{
if((i==j) || (j==0))
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
// display the pascal triangle //
for(i=0;i<size;i++)
{
for(j=1;j<=15-i;j++)
System.out.print (" ");
for(j=0;j<=i;j++)
System.out.print (a[i][j]+" ");
System.out.println ();
}
}
}
2. Assign 10 numbers into an array and then find the Highest and Lowest value in the array.
public class maxmin
{
public static void main()
{
int i;
int a[]={1,2,3,4,5,6,7,8,9,10};
int max=a[0],min=a[0];
for(i=0;i<10;i++)
{
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
System.out.println ("Maximum = "+max);
System.out.println ("Minimum = "+min);
}
}
3. WAP to accept 10 numbers in an array. Then ask the users to enter a Number and search it in array (using binary search method). Display the position if number is found otherwise display "not found".
public class binarySearch
{
public static void main(int x)
{
int a[]={4,6,10,13,15,18,20};
int l,h,m;
char ans='n';
l=0;
h=a.length-1;
m=(l+h)/2;
while(h>=l && ans=='n')
{
m=(l+h)/2;
if(a[m]>x)
h=m-1;
else
if(a[m]<x)
l=m+1;
else
{
ans='y';
break;
}
}
if(ans=='y')
System.out.println ("found in position "+(m+1));
else
}
}
4. Assign numbers into an array of size 4X4. Then calculate the sum of each row total and column total.
public class RowColTotal
{
public static void main()
{
int a[][]={{4,5,8,1},{2,8,7,4},{3,4,5,6},{1,2,7,8}};
int i,j,Row=0,Col=0;
for(i=0;i<4;i++)
{
Row=0;
for(j=0;j<4;j++)
{
Row+=a[i][j];
}
System.out.println ("Row "+(i+1)+" total is : "+Row);
}
for(i=0;i<4;i++)
{
Col=0;
for(j=0;j<4;j++)
{
Col+=a[j][i];
}
System.out.println ("Column "+(i+1)+" total is : "+Col);
}
}
}
5. Assign numbers into two array A & B of size 5. The program should create another array T that finds the common element in both the arrays. (Assume that the numbers are not repeated in same array).
public class setIntersection
{
public static void main()
{
int i,j,k=0;
int a[] = {1,2,3,4,5}, b[] = {1,4,5,7,8};
int t[]=new int[5];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i]==b[j])
{
t[k]=a[i];
k++;
}
}
}
for(i=0;i<k;i++)
System.out.println (t[i]);
}
}
[Set III]
Time: 1 Hrs                                                                            Marks: 5x5=25
1. Create 3 arrays named A, P & N. Assign 15 numbers in array A. Shift all the positive numbers in array P and all the negative numbers in array N. Finally print the array P & N.
import java.io.DataInputStream;
import java.io.IOException;
public class ques14
{
public static void main()throws IOException
{
DataInputStream x=new DataInputStream(System.in);
int a[]=new int[5];
int p[]=new int[5];
int n[]=new int[5];
int i,k=0,l=0;
for(i=0;i<5;i++)
{
System.out.print ("enter a number: ");
}
for(i=0;i<5;i++)
{
if(a[i]>0 && a[i]%2==0)
{
p[k]=a[i];
k++;
}
}
for(i=0;i<5;i++)
{
if(a[i]<0 && a[i]%2!=0)
{
n[l]=a[i];
l++;
}
}
System.out.print ("List of positive numbers");
for(i=0;i<k;i++)
System.out.println (p[i]);
System.out.print ("List of negative numbers");
for(i=0;i<l;i++)
System.out.println (n[i]);
}
}
2. Accept numbers into an array of size 10. Then accept a number and search that number in array. If the number is present in array, then display the array element number where number is found. In case of multiple found display all the positions. Display a proper message if the number is not present in array.
public class ques13
{
public static void main(int number)
{
int i,j;
boolean flag=false;
int a[] = {2,6,8,5, 1,4,8,0,2,7};
for(i=0;i<10;i++)
{
if(a[i]==number)
{
System.out.println ("Match found in position:" + (i+1));
flag=true;
}
}
if(flag == false)
}
}
3. WAP to generate a Pascal Triangle in which each number in the row is the sum of the two numbers immediately above it. The program should print the first 5 rows of the triangle.
public class ques39
{
public static void main()
{
int size=5;
int a[][]=new int[size][size];
int i,j;
a[0][0]=1;
// prepare the pascal triangle //
for(i=1;i<size;i++)
{
for(j=0;j<size;j++)
{
if((i==j) || (j==0))
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
// display the pascal triangle //
for(i=0;i<size;i++)
{
for(j=1;j<=15-i;j++)
System.out.print (" ");
for(j=0;j<=i;j++)
System.out.print (a[i][j]+" ");
System.out.println ();
}
}
}
4. Accept the name and total marks of 15 students from user. Then display the list according to their merit. (Use Bubble sort technique)
import java.io.DataInputStream;
import java.io.IOException;
public class ques19
{
public static void main() throws IOException
{
DataInputStream stdin = new DataInputStream(System.in);
String n[]=new String[5];
int m[]=new int[5];
int i,j,k;
for(i=0;i<5;i++)
{
System.out.print ("Enter Name : ");
System.out.print ("Enter Comp. Sc. Marks : ");
}
// Sort the array in Descending order
String x=null;
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(m[i]<m[j])
{
k=m[i]; m[i]=m[j]; m[j]=k;
x=n[i]; n[i]=n[j]; n[j]=x;
}
}
}
// display Merit List
System.out.println ("Name \t\t Marks");
for(i=0;i<5;i++)
System.out.println (n[i]+"\t\t"+m[i]);
} // end of method
}
5. WAP to assign numbers into a 4X3 matrix. Display the original matrix and display the matrix in transpose form.
// Transpose a Matrix //
public class ques32
{
public static void main()
{
int a[][] = {{4,5,8,1}, {2,8,7,4}, {3,4,5,6}};
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
System.out.print (a[i][j]+" ");
System.out.println ();
}
System.out.println ();
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
System.out.print (a[j][i]+" ");
System.out.println ();
}
}
}
[Set IV]
Time: 1 Hrs                                                                            Marks: 3x5=15
1. WAP to accept ten elements in an array and transfer them in reverse form in a different array.
import java.io.*;
public class arrReverse
{
public static void main() throws IOException
{
DataInputStream in = new DataInputStream(System.in);
int n[] = new int[10];
int r[] = new int[10];
for(int i=0;i<10;i++)
{
System.out.print ("Enter a number : ");
}
for(int i=0,j=9;i<10;i++,j--)
{
r[i] = n[j];
}
for(int i=0;i<10;i++)
{
System.out.println (r[i]);
}
}
}
2. WAP to accept names of the 10 politicians and their party in two separate arrays. Ask the users to enter the name of the party and Print all the names of the politicians who belong to that party.
import java.io.*;
public class ques23
{
public static void main() throws IOException
{
DataInputStream stdin = new DataInputStream(System.in);
String party[] = new String[10];
String poli[] = new String[10];
String search;
boolean flag = false;
for(int i=0;i<10;i++)
{
System.out.print ("Enter Party Name : ");
System.out.print ("Enter Politician Name : ");
}
System.out.print ("Enter Searching Party Name : ");
System.out.println ("Name of Politician's");
for(int i=0;i<5;i++)
if(search.compareTo(party[i]) =  = 0)
System.out.println (poli[i]);
}
}
3. WAP to input the names and computer marks of 50 students in arrays then Calculate and display:
(i) The subject average marks.
(ii) The highest mark and the name of the student.
import java.io.*;
class highMarks
{
public static void main()throws IOException
{
int marks[] = new int[50];
String name[] = new String[50];
String HighName;
int HighMarks,total = 0;
for(int i=0;i<50;i++)
{
System.out.println ("Enter Name and Marks");
}
System.out.println ("Enter name and marks");
HighName = name[0];
HighMarks = marks[0];
total = HighMarks;
for(int i=1;i<50;i++)
{
if(marks[i]>HighMarks)
{
HighMarks = marks[i];
HighName = name[i];
}
total = total+marks[i];
}
double avg = total/50.0;
System.out.println ("Subject average marks is"+avg);
System.out.println ("Highest marks is "+HighMarks+"and the scorer is"+HighName);
}
}
[Set V]
Time: 1 Hrs                                                                            Marks: 4x5=20
1. Assign/Store 10 numbers into an array and then find the Highest and Lowest value in the array with their position.
public class MaxMinPos
{
public static void main()
{
int i;
int a[] = {3,7,2,4,1,6,7,10,9,8};
int max = a[0],min = a[0];
int maxPos = 0,minPos = 0;
for(i=0;i<10;i++)
{
if(a[i]>max)
{
max = a[i];
maxPos = i;
}
if(a[i]<min)
{
min = a[i];
minPos = i;
}
}
System.out.println ("Maximum No. = "+max);
System.out.println ("Position = "+maxPos);
System.out.println ("Minimum No. = "+min);
System.out.println ("Position = "+minPos);
}
}
2. Assign numbers into two array A & B of size 5. Then find common element between arrays and transfer them in another array.
public class ques8
{
public static void main()
{
int i,j,x = 0;
int a[] = {1,2,3,4,5};
int b[] = {3,4,5,6,7};
int c[] = new int[5];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i] =  = b[j])
{
c[x] = a[i];
x++;
}
}
}
for(i=0;i<x;i++)
System.out.println (c[i]);
}
}
3. Write a program that accept 10 integers from user in such a way that no negative and zero accepted to array.
import java.io.*;
public class nonNegative
{
public static void arr() throws IOException
{
DataInputStream stdin = new DataInputStream(System.in);
int n[] = new int[10];
int i = 0;
do
{
System.out.print ("Enter a number : ");
if(a>0)
{
n[i] = a;
i++;
}
}while(i<10);
for(i = 0;i<10;i++)
System.out.println ("The number is : " + n[i]);
}
}
4. Create an array of size 10 then accept 9 numbers into it. Then accept another number and its position where you want to insert it. Insert the given number in given position then Display the array.
import java.io.DataInputStream;
import java.io.IOException;
public class ques10
{
public static void main()throws IOException
{
DataInputStream x = new DataInputStream(System.in);
int a[] = new int[10];
int i,p,n;
for(i=0;i<9;i++)
{
System.out.print ("enter a number: ");
}
System.out.print ("Enter the number: ");
System.out.print ("Enter position : ");
for(i=9;i>=p;i--)
a[i] = a[i-1];
a[p-1] = n;
for(i=0;i<10;i++)
System.out.println (a[i]);
}
}
[Set VI]
Time: 1 Hrs                                                                            Marks: 6x5=30
1. Write a Program to generate a Pascal Triangle in which each number in the row is the sum of the two numbers immediately above it. The program should print the first 5 rows of the triangle.
public class pascal
{
public static void main(int n)
{
int pas[] = new int[n+1];
pas[0] = 1;
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
System.out.print (pas[j]+" ");
System.out.println ();
for(int j=i+1;j>0;j--)
pas[j] = pas[j]+pas[j-1];
}
}
}
2. Assign numbers into an array of size 4X4. Then calculate and print the sum of individual diagonals, sum of each row and sum of each column.
public class RowColDiagTotal
{
public static void main()
{
int a[][] = {{4,5,8,1}, {2,8,7,4}, {3,4,5,6}, {1,2,7,8}};
int i,j, Row=0, Col=0, Diag1=0, Diag2=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j)
Diag1+=a[i][j];
if((i+j)==3)
Diag2+=a[i][j];
}
}
System.out.println ("Sum of Left diagonals :"+Diag1);
System.out.println ("Sum of RIght diagonals :"+Diag2);
for(i=0;i<4;i++)
{
Row=0;
for(j=0;j<4;j++)
{
Row+=a[i][j];
}
System.out.println ("Row "+(i+1)+" total is : "+Row);
}
for(i=0;i<4;i++)
{
Col=0;
for(j=0;j<4;j++)
{
Col+=a[j][i];
}
System.out.println ("Column " + (i+1) + " total is : " + Col);
}
}
}
3. Write a Program to Store 10 numbers in (Descending order) an array. Then ask the users to enter a Number and search it in array (using binary search method). Display the position if number is found otherwise display "not found".
public class binarySearchRev
{
public static void main(int x)
{
int a[] = {20,18,15, 13,10,6,4};
int l=0, h=a.length-1;
boolean flag=false;
int m=(l+h)/2;
while(h>=l)
{
m=(l+h)/2;
if(a[m]>x)
l=m+1;
else
if(a[m]<x)
h=m-1;
else
{
flag=true;
break;
}
}
if(flag)
System.out.println ("found in position " +(m+1));
else
}
}
4. Accept the name and total marks of 15 students from user. Then display the list according to their merit. (Use Bubble sort)
import java.io.*;
public class ques19
{
public static void main() throws IOException
{
DataInputStream stdin = new DataInputStream(System.in);
String n[]=new String[5];
int m[]=new int[5];
int i,j,k;
for(i=0;i<5;i++)
{
System.out.print ("Enter Name : ");
System.out.print ("Enter Comp. Sc. Marks : ");
}
String x="";
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(m[i]<m[j])
{
k=m[i]; m[i]=m[j]; m[j]=k;
x=n[i]; n[i]=n[j]; n[j]=x;
}
}
}
System.out.println ( "Name \t\t Marks" );
for(i=0;i<5;i++)
System.out.println (n[i] + "\t\t" + m[i]);
}
}
5. Write a Program to accept Names of 20 students (from user) in an Array. Then arrange names alphabetically using Selection sort method.
import java.io.*;
public class nameSort
{
public static void main() throws IOException
{
DataInputStream stdin = new DataInputStream(System.in);
String n[]=new String[5];
String small,k;
int i,j,pos,l=n.length;
for(i=0;i<5;i++)
{
System.out.print ("Enter Name : ");
}
for(i=0;i<l-1;i++)
{
small=n[i];
pos=i;
for(j=i+1;j<l;j++)
{
if(n[j].compareTo(small)<0)
{
small=n[j];
pos=j;
}
}
k=n[i];
n[i]=n[pos];
n[pos]=k;
}
for(j=0;j<5;j++)
System.out.println (n[j]);
}
}
6. A metropolitan hotel has ten floors and each floor having fifty rooms. Write a program that will display the room and floor number of a given Visitor.
import java.io.*;
public class Hotel
{
public static void main() throws IOException
{
DataInputStream stdin = new DataInputStream(System.in);
String n[][]=new String[10][50];
String ser;
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<50;j++)
{
System.out.print ("Enter Visitor Name : ");