Array
Array
Question 1
int a[-40]
int a[40] ✓
None
Question 2
10th
9th
11th ✓
None
Question 3
packets
blocks
subscripts ✓
compartments
Question 4
subscripted variable ✓
actual variable
compound variable
none
Question 5
dots
element name
index number ✓
none
Question 6
Indicate the error message which displays, if the following statement is executed :
int a[5] = {28,32,45,68,12};
Insufficient cells
None ✓
Question 7
assigns 37 to code[1] ✓
assigns 25 to code[1]
assigns 38 to code[3]
assigns 42 to code[0]
Question 8
from 1 to 50
from 0 to 49 ✓
from 1 to 51
none
Question 9
m.size of (a)
m.elements of (m)
m.length ✓
None
Question 10
A Single Dimensional array contains N elements. What will be the last subscript?
N-1 ✓
N+1
None
Question 1
Output
46
Explanation
Question 2
a[0]=23;
a[3]=a[1];
int c= a[0]+a[1];
System.out.println("Sum = "+c);
Output
Sum = 27
Explanation
a[0]=23 assigns 23 to the first element of the array. a[3]=a[1] assigns the value of second element of the
array which is 4 to the fourth element of the array. After the execution of these two statements array
looks like this:
{23, 4, 6, 4, 10}
a[0]+a[1] ⇒ 23 + 4 ⇒ 27
Question 3
System.out.println(a[2+1]);
Output
12
Explanation
a[2+1] ⇒ a[3] ⇒ 12
Question 4
int a[4]={2,4,6,8};
for(i=0;i<=1;i++)
s=a[i]+a[3-i];
System.out.println(s);
Output
10
10
Explanation
i Output Remark
a[0] + a[3]
0 ⇒2+8 First Iteration
⇒10
a[1] + a[2]
1 ⇒4+6 Second Iteration
⇒10
A dimensional array is a structure created in the memory to represent a number of values of the same
data type with the variables having the same variable name along with different subscripts.
Question 2
Question 3
Variables are useful for keeping track of a single piece of information but as we collect more and more
information, keeping the variables organized can be complicated. In such situations, we need arrays to
solve the problems in a much better and efficient way.
Question 4
The data type that represents a number of similar or different data under single declaration is called as
composite data type. An array is a group or a collection of same type of variables. Hence, Array is a
composite data type.
Question 5
A Single Dimensional Array contains one row and one or more columns. The syntax of declaring a Single
Dimensional Array is:
<type> <array-variable>[] = new <type>[<size>];
OR
<type> [] <array-variable> = new <type>[<size>];
Double Dimensional Array contains multiple rows and multiple columns. The syntax of declaring a
Double Dimensional Array is:
<type> <array-variable>[][] = new <type>[<rows>][<columns>];
OR
<type> [][] <array-variable> = new <type>[<rows>][<columns>];
Subscript is the index of the element in the array whereas Subscripted variable is the name of the array
when it is used with a subscript to access a single element of the array.
Question 2
char a[5] is an array of char data type that can hold 5 characters whereas int a[5] is an array of int data
type that can hold 5 integer values.
Question 3
An ordinary variable can hold only one value whereas an array variable can refer to a group of values of
the same data type by using a subscript.
Question 4
Sorting Searching
Question 5
Question 6
Question 7
length length()
Question 1
Write a program in Java to store 20 numbers (even and odd numbers) in a Single Dimensional Array
(SDA). Calculate and display the sum of all even numbers and all odd numbers separately.
import java.util.Scanner;
System.out.println("Enter 20 numbers");
arr[i] = in.nextInt();
if (arr[i] % 2 == 0)
evenSum += arr[i];
else
oddSum += arr[i];
}
Output
Question 2
Write a program in Java to store 20 temperatures in °F in a Single Dimensional Array (SDA) and display
all the temperatures after converting them into °C.
Hint: (c/5) = (f - 32) / 9
import java.util.Scanner;
arr[i] = in.nextDouble();
System.out.println(tc);
Output
Question 3
Write a program in Java to store 10 numbers (including positive and negative numbers) in a Single
Dimensional Array (SDA). Display all the negative numbers followed by the positive numbers without
changing the order of the numbers.
Sample Input:
n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
Sample Output: -32, -41, -19, 44, 15, 21, 54, 61, 71, 52
import java.util.Scanner;
System.out.println("Enter 10 numbers");
arr[i] = in.nextInt();
if (arr[i] < 0)
if (arr[i] >= 0)
Output
Question 4
Write a program in Java to store 20 numbers in a Single Dimensional Array (SDA). Display the numbers
which are prime.
Sample Input:
n[0] n[1] n[2] n[3] n[4] n[5] ... n[16] n[17] n[18] n[19]
45 65 77 71 90 67 ... 82 19 31 52
import java.util.Scanner;
System.out.println("Enter 20 numbers");
arr[i] = in.nextInt();
System.out.println("Prime Numbers:");
int c = 0;
if (arr[i] % j == 0) {
c++;
if (c == 2)
Output
Question 5
Write a program to accept name and total marks of N number of students in two single subscript arrays
name[ ] and totalmarks[ ].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student's total marks with the average.
[deviation = total marks of a student - average]
import java.util.Scanner;
int n = in.nextInt();
int grandTotal = 0;
in.nextLine();
name[i] = in.nextLine();
totalmarks[i] = in.nextInt();
grandTotal += totalmarks[i];
+ (totalmarks[i] - avg));
Output
Question 6
..... .....
..... .....
..... .....
Write a program to input the names and marks of the students in the subject.
Calculate and display:
(a) The subject average marks (subject average marks = subject total/50).
(b) The highest marks in the subject and the name of the student. (The maximum marks in the subject
are 100.)
import java.util.Scanner;
int total = 0;
name[i] = in.nextLine();
marks[i] = in.nextInt();
total += marks[i];
in.nextLine();
}
int hIdx = 0;
hIdx = i;
Output
Question 7
From 80 to 100 A
From 60 to 79 B
From 40 to 59 C
Less than 40 D
import java.util.Scanner;
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
s1[i] = in.nextInt();
s2[i] = in.nextInt();
s3[i] = in.nextInt();
s4[i] = in.nextInt();
s5[i] = in.nextInt();
s6[i] = in.nextInt();
g[i] = 'D';
g[i] = 'C';
g[i] = 'B';
else
g[i] = 'A';
}
System.out.println();
System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ p[i] + "\t"
+ g[i]);
Output
Question 8
Write a program to accept a list of 20 integers. Sort the first 10 numbers in ascending order and next the
10 numbers in descending order by using 'Bubble Sort' technique. Finally, print the complete list of
integers.
import java.util.Scanner;
System.out.println("Enter 20 numbers:");
arr[i] = in.nextInt();
arr[j + 1] = arr[j];
arr[j] = t;
arr[j + 1] = arr[j];
arr[j] = t;
System.out.println("\nSorted Array:");
Output
Question 9
The class teacher wants to store the marks obtained in English, Maths and Science of her class having 40
students. Write a program to input marks in Eng, Science and Maths by using three single dimensional
arrays. Calculate and print the following information:
(i) Average marks secured by each student.
(ii) Class average in each subject.
[Hint: Class average is the average marks obtained by 40 students in a particular subject.]
import java.util.Scanner;
english[i] = in.nextInt();
maths[i] = in.nextInt();
science[i] = in.nextInt();
}
int engTotal = 0, mathsTotal = 0, sciTotal = 0;
engTotal += english[i];
mathsTotal += maths[i];
sciTotal += science[i];
Output
Question 10
Write a program in Java to accept 20 numbers in a single dimensional array arr[20]. Transfer and store
all the even numbers in an array even[ ] and all the odd numbers in another array odd[ ]. Finally, print
the elements of both the arrays.
import java.util.Scanner;
int i = 0;
System.out.println("Enter 20 numbers:");
arr[i] = in.nextInt();
if (arr[i] % 2 == 0)
even[eIdx++] = arr[i];
else
odd[oIdx++] = arr[i];
System.out.println("Even Numbers:");
System.out.println("\nOdd Numbers:");
Output
Question 11
Write a program to store 20 numbers in a Single Dimensional Array (SDA). Now, display only those
numbers that are perfect squares.
n[0] n[1] n[2] n[3] n[4] n[5] ... n[16] n[17] n[18] n[19]
12 45 49 78 64 77 ... 81 99 45 33
import java.util.Scanner;
System.out.println("Enter 20 numbers");
arr[i] = in.nextInt();
double sr = Math.sqrt(arr[i]);
if ((sr - Math.floor(sr)) == 0)
Output
Question 12
To get promotion in a Science stream, a student must pass in English and should pass in any of the two
subjects (i.e.; Physics, Chemistry or Maths). The passing mark in each subject is 35. Write a program in a
Single Dimension Array to accept the roll numbers and marks secured in the subjects for all the
students. The program should check and display the roll numbers along with a message whether
"Promotion is Granted" or "Promotion is not Granted". Assume that there are 40 students in the class.
import java.util.Scanner;
studentDetails[i] = in.nextInt();
studentDetails[i+1] = in.nextInt();
studentDetails[i+2] = in.nextInt();
studentDetails[i+3] = in.nextInt();
studentDetails[i+4] = in.nextInt();
System.out.println("Promotion is granted.");
else {
Output
Question 13
Write a program to accept 10 different decimal numbers (double data type) in a Single Dimensional
Array (say, A). Truncate the fractional part of each number of the array A and store their integer part in
another array (say, B).
import java.util.Scanner;
a[i] = in.nextDouble();
b[i] = (int)a[i];
System.out.println("Truncated numbers");
Output
Question 14
The annual examination result of 50 students in a class is tabulated in a Single Dimensional Array (SDA)
is as follows:
Write a program to read the data, calculate and display the following:
(a) Average marks obtained by each student.
(b) Print the roll number and the average marks of the students whose average is above. 80.
(c) Print the roll number and the average marks of the students whose average is below 40.
import java.util.Scanner;
rollNo[i] = in.nextInt();
sA[i] = in.nextInt();
sB[i] = in.nextInt();
Output
Question 15
Write a program to store 6 elements in an array P and 4 elements in an array Q. Now, produce a third
array R, containing all the elements of array P and Q. Display the resultant array.
P[ ] Q[ ] R[ ]
4 19 4
6 23 6
1 7 1
2 8 2
3 3
10 10
19
23
import java.util.Scanner;
int i = 0;
P[i] = in.nextInt();
Q[i] = in.nextInt();
i = 0;
R[i] = P[i];
i++;
int j = 0;
R[i++] = Q[j++];
}
}
Output
Question 16
Write a program to accept the year of graduation from school as an integer value from the user. Using
the binary search technique on the sorted array of integers given below, output the message "Record
exists" if the value input is located in the array. If not, output the message "Record does not exist".
Sample Input:
n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
1982 1987 1993 1996 1999 2003 2006 2007 2009 2010
import java.util.Scanner;
public class KboatGraduationYear
int n[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
while (l <= h) {
int m = (l + h) / 2;
if (n[m] == year) {
idx = m;
break;
l = m + 1;
else {
h = m - 1;
if (idx == -1)
else
System.out.println("Record exists");
}
}
Output
Question 17
Write a program to input and store roll numbers, names and marks in 3 subjects of n number of
students in five single dimensional arrays and display the remark based on average marks as given
below:
85 — 100 Excellent
75 — 84 Distinction
60 — 74 First Class
40 — 59 Pass
import java.util.Scanner;
int n = in.nextInt();
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
s1[i] = in.nextInt();
s2[i] = in.nextInt();
s3[i] = in.nextInt();
}
System.out.println("Roll No\tName\tRemark");
String remark;
remark = "Poor";
remark = "Pass";
remark = "Distinction";
else
remark = "Excellent";
System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ remark);
Output
Question 18
A double dimensional array is defined as N[4][4] to store numbers. Write a program to find the sum of
all even numbers and product of all odd numbers of the elements stored in Double Dimensional Array
(DDA).
Sample Input:
12 10 15 17
30 11 32 71
17 14 29 31
41 33 40 51
Sample Output:
Sum of all even numbers: ..........
Product of all odd numbers: ..........
import java.util.Scanner;
N[i][j] = in.nextInt();
if (N[i][j] % 2 == 0)
evenSum += N[i][j];
else
oddProd *= N[i][j];
}
System.out.println("Sum of all even numbers = " + evenSum);
Output
Question 19
A Departmental Shop has 5 stores and 6 departments. The monthly sale of the department is kept in the
Double Dimensional Array (DDA) as m[5][6]. The Manager of the shop wants to know the total monthly
sale of each store and each department at any time. Write a program to perform the given task.
(Hint: Number of stores as rows and Number of departments as columns.)
import java.util.Scanner;
(j + 1) + ": ");
m[i][j] = in.nextInt();
long storeSale = 0;
storeSale += m[i][j];
System.out.println("Store " + (i + 1)
}
System.out.println("\nMonthly Sale by Department: ");
long deptSale = 0;
deptSale += m[j][i];
System.out.println("Department " + (i + 1)
Output
Question 20
A Metropolitan Hotel has 5 floors and 10 rooms in each floor. The names of the visitors are entered in a
Double Dimensional Array (DDA) as M[5][10].The Hotel Manager wants to know from the "Enquiry"
about the position of a visitor (i.e. floor number and room number) as soon as he enters the name of the
visitor. Write a program in Java to perform the above task.
import java.util.Scanner;
int i = 0, j = 0;
(j + 1) + ": ");
M[i][j] = in.nextLine();
if (M[i][j].equals(guest)) {
found = true;
break;
if (found)
break;
if (found)
else
System.out.println(guest +
Output
Question 21
A Class Teacher wants to keep the records of 40 students of her class along with their names and marks
obtained in English, Hindi, Maths, Science and Computer Science in a Double Dimensional Array (DDA) as
M[40][5]. When the teacher enters the name of a student as an input, the program must display the
name, marks obtained in the 5 subjects and the total. Write a program in Java to perform the task.
import java.util.Scanner;
int i = 0, j = 0;
System.out.print("Name: ");
names[i] = in.nextLine();
marks[i][j] = in.nextInt();
in.nextLine();
}
System.out.print("\nEnter name of the student to search: ");
if (names[i].equals(studentName))
break;
if (i == TOTAL_STUDENTS) {
else {
int total = 0;
System.out.println("Marks in " +
total += marks[i][j];
Output
Question 22
If arrays M and M + N are as shown below, write a program in Java to find the array N.
int arrM[][] = {
{-1, 0, 2},
{4, 3, -1}
};
int arrSum[][] = {
{-6, 9, 4},
{4, 5, 0},
};
}
System.out.println("Array N:");
System.out.print(arrN[i][j]);
System.out.print(' ');
System.out.println();
Output
Video Explanations