Array
Array
1. int a[-40]
2. int a[40]
3. float a[0 - 40]
4. None
Question 2
th
1. 10
th
2. 9
th
3. 11
4. None
Question 3
1. packets
2. blocks
3. subscripts
4. compartments
Question 4
1. subscripted variable
2. actual variable
3. compound variable
4. none
Question 5
1. dots
2. element name
3. index number
4. none
Question 6
Indicate the error message which displays, if the following statement is executed :
int a[5] = {28,32,45,68,12};
1. Insufficient cells
2. Array index out of bounce
3. Elements exceeding cells
4. None
Question 7
1. assigns 37 to code[1]
2. assigns 25 to code[1]
3. assigns 38 to code[3]
4. assigns 42 to code[0]
Question 8
1. from 1 to 50
2. from 0 to 49
3. from 1 to 51
4. none
Question 9
1. m.size of (a)
2. m.elements of (m)
3. m.length
4. None
Question 10
A Single Dimensional array contains N elements. What will be the last subscript?
1. N-1
2. N
3. N+1
4. None
Output
4 6
Explanation
Question 2
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
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
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>];
(b) Double Dimensional Array
Double Dimensional Array contains multiple rows and multiple columns. The syntax of declaring a Double
Dimensional Array is:
<type> <array-variable>[][]
variable>[][] = new <type>[<rows>][<columns>];
OR
<type> [][] <array-variable>
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]
[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 variabl
variablee can refer to a group of values of the
same data type by using a subscript.
Question 4
Sorting Searching
Sorting means to arrange the elements of the array in ascending Searching means to search for a term or value in an
or descending order. array.
Linear search and Binary search are examples of search Bubble sort and Selection sort are examples of
Sorting Searching
Question 5
Each element of the array is checked against the target Array is successively divided into 2 halves and the target
value until the element is found or end of the array is element is searched either in the first half or in the second
reached half
Question 6
Selection Sort selects the smallest element from unsorted sub-array Bubble Sort compares adjacent elements and
and swaps it with the leftmost unsorted element. swaps them if they are in wrong order.
Question 7
length length()
length is an attribute i.e. a data member of array. length() is a member method of String class.
It gives the length of an array i.e. the number of elements stored in an It gives the number of characters present in a
array. string.
Solutions to Unsolved Java Programs
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");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
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;
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");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
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("Prime Numbers:");
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 1)
continue;
if (isPrime)
System.out.print(arr[i] + ", ");
}
}
}
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;
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;
Output
Question 7
From 80 to 100 A
From 60 to 79 B
Percentage Marks Grade
From 40 to 59 C
Less than 40 D
import java.util.Scanner;
System.out.println();
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;
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;
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;
System.out.println("Enter 20 numbers:");
for (i = 0; i < NUM_COUNT; i++) {
arr[i] = in.nextInt();
}
System.out.println("Even Numbers:");
for (i = 0; i < eIdx; i++) {
System.out.print(even[i] + " ");
}
System.out.println("\nOdd Numbers:");
for (i = 0; i < oIdx; i++) {
System.out.print(odd[i] + " ");
}
}
}
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");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
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;
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;
System.out.println("Truncated numbers");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + ", ");
}
}
}
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;
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;
i = 0;
while(i < P.length) {
R[i] = P[i];
i++;
}
int j = 0;
while(j < Q.length) {
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
{
public void searchGraduationYear() {
Scanner in = new Scanner(System.in);
int n[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
if (idx == -1)
System.out.println("Record does not exist");
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:
Average Marks Remark
85 — 100 Excellent
75 — 84 Distinction
60 — 74 First Class
40 — 59 Pass
import java.util.Scanner;
System.out.println("Roll No\tName\tRemark");
for (int i = 0; i < n; i++) {
String remark;
if (avg[i] < 40)
remark = "Poor";
else if (avg[i] < 60)
remark = "Pass";
else if (avg[i] < 75)
remark = "First Class";
else if (avg[i] < 85)
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;
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;
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;
if (found)
break;
}
if (found)
System.out.println(guest + " is in room number "
+ (j + 1) + " on floor number " + (i + 1));
else
System.out.println(guest +
" is not staying at this hotel");
}
}
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;
if (i == TOTAL_STUDENTS) {
System.out.println("Record for " + studentName
+ " not found");
}
else {
System.out.println("Name: " + names[i]);
int total = 0;
for (j = 0; j < TOTAL_SUBJECTS; j++) {
System.out.println("Marks in " +
SUBJECT_NAMES[j] + ": " + marks[i][j]);
total += marks[i][j];
}
System.out.println("Total Marks: " + total);
}
}
}
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},
{-3, -1, 6},
{4, 3, -1}
};
int arrSum[][] = {
{-6, 9, 4},
{4, 5, 0},
{1, -2, -3}
};
System.out.println("Array N:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arrN[i][j]);
System.out.print(' ');
}
System.out.println();
}
}
}
Output