Chapter 12 Arrays
Chapter 12 Arrays
Arrays
Class 10 - APC Understanding Computer Applications
with BlueJ
Question 1
1. int a[-40]
2. int a[40]
3. float a[0 - 40]
4. None
Answer
int a[40]
Question 2
1. 10th
2. 9th
3. 11th
4. None
Answer
11th
Reason — Array indexes start from 0. So, a[10] refers to the 11th element of the array.
Question 3
1. packets
2. blocks
3. subscripts
4. compartments
Answer
subscripts
Reason — Cell numbers of a dimensional array are also known as subscripts.
Question 4
1. subscripted variable
2. actual variable
3. compound variable
4. none
Answer
subscripted variable
Reason — A dimensional array is also known as subscripted variable because each element of the array is
represented by using a common variable name along with a single subscript.
Question 5
1. dots
2. element name
3. index number
4. none
Answer
index number
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 bounds
3. Elements exceeding cells
4. None
Answer
None
Reason — The statement int a[5] = {28,32,45,68,12}; is perfectly correct and will not generate any error
message.
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]
Answer
assigns 37 to code[1]
Reason — The statement int code[ ]= {25,37,38,42}; assigns 37 to code[1] as the array index begins from 0.
So the second value (37) will be assigned to code[1].
Question 8
1. from 1 to 50
2. from 0 to 49
3. from 1 to 51
4. none
Answer
from 0 to 49
Reason — The elements of array[50] are numbered from 0 to 49 as the array index begins from 0.
Question 9
1. m.sizeof (a)
2. m.elementsof (m)
3. m.length
4. None
Answer
m.length
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
Answer
N-1
Reason — The array subscripts range from 0 to (n-1), where n is the number of elements in the array.
Each element of a single dimensional array is accessed using variable along with subscript enclosed within square
bracket.
Question 2
Question 3
To access the elements in reverse order, the single dimensional array is traversed starting with last index.
Question 4
Question 5
The last index of a single dimensional array will be 7, if it is declared to store 8 elements.
Question 1
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:
a[0] + a[1] ⇒ 23 + 4 ⇒ 27
{23, 4, 6, 4, 10}
Question 3
Output
12
Explanation
a[2+1] ⇒ a[3] ⇒ 12
Question 4
Output
10
10
Explanation
i Output Remark
⇒2+8
a[0] + a[3]
⇒10
0 First Iteration
⇒4+6
a[1] + a[2]
⇒10
1 Second Iteration
Question 1
Java allows storing a number of similar type of data using a common variable name such that each element is
accessed with the help of a subscript or cell number. This is done with a purpose that any operation can be carried
commonly on all the data items. You can search a number by dividing the sorted elements in two halves in each
iteration. Beyond searching, the elements can also be sorted using different techniques. One technique allows us to
choose the lowest element starting with a specific subscript in each pass and place it in appropriate position whereas,
in other technique, consecutive elements are compared and a number of exchanges are made in each pass. We can
also join two or more arrays to form a single set.
(a) What is the term used for a common variable storing a number of similar data?
1. Numeric variable
2. Static variable
3. Subscripted variable
4. Local variable
(b) Which of the following techniques is used for searching an element by dividing the sorted list of elements into
two halves in each iteration?
1. Linear search
2. Binary search
3. Sequential search
4. None of the above
(c) Which of the following sorting techniques finds the lowest element in each pass and places it in appropriate
position?
1. Selection sort
2. Binary sort
3. Bubble sort
4. Insertion sort
(d) Which of the following term is used for joining two or more arrays to form a single array?
1. Joining
2. Collecting
3. Merging
4. Sorting
Answer
(d) Merging
Question 1
Answer
A dimensional array is a structure created in the memory to represent a number of data values of the same data type
having the same variable name along with different subscripts.
Question 2
Answer
Question 3
Answer
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
Answer
The data type that represents a number of similar or different data under single declaration is called as composite
data type. An array is the concept of using a number of variables declared with the same data type. Hence, an array
is said to be a composite data type.
Question 5
Answer
(a) Single Dimensional Array — A single dimensional array is a structure that is represented along X-axis by using
a number of variables with the same name having different subscripts. Each element of the array is represented by
using a common variable name along with a single subscript.
Question 1
Answer
Subscript is the index of the Subscripted variable is the name of the array when it is used with a subscript
element in the array. to access a single element of the array.
Question 2
Answer
char a[5] is an array of char data type. int a[5] is an array of int data type.
This array can hold 5 characters. This array can hold 5 integer values.
Question 3
Answer
An ordinary variable can hold only An array variable can refer to a group of values of the same data type by
one value. using a subscript.
Question 4
Answer
Sorting Searching
Sorting means to arrange the elements of the array in Searching means to search for a term or value in
ascending or descending order. an array.
Bubble sort and Selection sort are examples of sorting Linear search and Binary search are examples of
techniques. search techniques.
Question 5
Answer
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
reached. second half.
Question 6
Answer
Selection sort Bubble sort
Selection Sort selects the smallest element from unsorted sub- Bubble Sort compares adjacent elements and
array and swaps it with the leftmost unsorted element. swaps them if they are in wrong order.
Performs lesser number of swaps to sort the same array relative Performs more number of swaps to sort the
to Bubble Sort. array.
Question 7
Answer
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 It gives the number of characters present in a
in an array. string.
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("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
System.out.println("Prime Numbers:");
for (int i = 0; i < arr.length; i++) {
int c = 0;
for (int j = 1; j <= arr[i]; j++) {
if (arr[i] % j == 0) {
c++;
}
}
if (c == 2)
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:
(a) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(b) Deviation of each student's total marks with the average.
[deviation = total marks of a student - average]
import java.util.Scanner;
Output
Question 6
From 80 to 100 A
From 60 to 79 B
From 40 to 59 C
Less than 40 D
import java.util.Scanner;
System.out.println();
Output
Question 7
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 8
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();
}
int eIdx = 0, oIdx = 0;
for (i = 0; i < NUM_COUNT; i++) {
if (arr[i] % 2 == 0)
even[eIdx++] = arr[i];
else
odd[oIdx++] = arr[i];
}
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 9
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 10
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 11
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 12
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
7
Input Input Output
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 13
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;
if (idx == -1)
System.out.println("Record does not exist");
else
System.out.println("Record exists");
}
}
Output
Question 14
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
Average Marks Remark
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 15
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 16
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 17
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 18
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 19
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