Arrays
Arrays
Arrays
Question 1
1. 0 to N
2. 0 to N-1
3. 1 to N
4. 1 to N - 1
Answer
0 to N-1
Reason — In Java, for an array having N elements, the subscripts start from 0 and go on till N - 1 (size
- 1).
Question 2
1. 25 bytes
2. 50 bytes
3. 100 bytes
4. None of these
Answer
50 bytes
Reason — The size of char data type is 2 bytes. Since A is an array of char type, the size of each
element of the array will be 2 bytes. The size of 25 elements will be 25 X 2 = 50 bytes.
Question 3
1. 50 bytes
2. 15 bytes
3. 100 bytes
4. 200 bytes
Answer
200 bytes
Reason — The size of int data type is 4 bytes. Since B is an array of int type, the size of each element
of the array will be 4 bytes. The array has 10 rows and 5 columns, thus total number of elements will
be 10 X 5 = 50. The size of 50 elements will be 50 X 4 = 200 bytes.
Question 4
1. 10
2. 20
3. 30
4. 50
Answer
30
Question 5
1. int number( );
2. float average[ ];
3. double[ ] marks;
4. counter int[ ];
Answer
float average[ ];
double[ ] marks;
Reason — The statements float average[ ]; and double[ ] marks; are valid as they follow the syntax
for declaration of an array.
int number( ); uses small brackets () instead of square brackets []. The correct statement will be int
number[];.
counter int[ ]; has exchanged the place of data type and array variable. The correct statement will
be int counter[];
Question 6
2. number[5] is undefined
3. number[2] is 0
4. number.length is 5
Answer
number[5] is undefined
Reason — The valid subscripts of an array of size N are from 0 to N - 1. Thus, subscripts of array
number will be from 0 to 4 (5 - 1).
Question 7
1. int x[ ] = int[10];
4. x = y = new int[10];
Answer
int x[ ] = int[10];
x = y = new int[10];
int i = new int(10);
Reason — int x[] = int[10]; doesn't contain the 'new' keyword used to initialize an array. The correct
statement will be int x[] = new int[10];
In the statement x = y = new int[10];, 'x' and 'y' are not declared as array variables. The correct
statement will be :
In the statement int i = new int(10);, 'i' is not declared as array variable and small brackets () are used
instead of square brackets []. The correct statement will be int i[] = new int[10];
The remaining statements follow the syntax used for declaring and initializing an array, thus they do
not have any error.
Question 8
1. 35
2. 26
3. 19
4. 76
5. 58
Answer
76
Reason — The valid subscripts of an array are from 0 to size - 1. Thus, A[0] = 35, A[1] = 26, A[2] = 19,
A[3] = 76 and A[4] = 58.
Question 9
Given that int z[ ][ ] = {{2, 6, 5}, {8, 3, 9}}; What will be value of z[1][0] and z[0][2] ?
1. 2 and 9
2. 8 and 5
3. 2 and 5
4. 6 and 3
Answer
8 and 5
Reason — Two-dimensional arrays are stored in a row-column matrix, where the first index indicates
the row and the second indicates the column.
The element stored at z[1][0] refers to the element stored in second row and first column, which is 8.
The element stored at z[0][2] refers to the element stored in first row and third column, which is 5.
Question 10
Given array 12, 3, 8, 5. What will be array like after two passes of selection sort ?
1. 12, 3, 8, 5
2. 3, 5, 8, 12
3. 3, 8, 5, 12
4. 3, 5, 12, 8
Answer
3, 5, 8, 12
Reason — Selection Sort is a sorting technique where next smallest (or next largest) element is found
in the array and moved to its correct position in each pass.
In the first pass, smallest element 3 will be found and swapped with the first position element 12.
The array elements after the first pass will be:
3, 12, 8, 5
In the second pass, the next smallest element 5 will be found and swapped with the second position
element 12.The array elements after the second pass will be:
3, 5, 8, 12
Question 11
Given an array 12, 3, 8, 5. What will be array like after two passes of bubble sort?
1. 12, 3, 8, 5
2. 3, 8, 12, 5
3. 3, 5, 8, 12
4. 12, 3, 5, 8
Answer
3, 8, 12, 5
Reason — The basic idea of bubble sort is to move the largest element to the highest index position
in the array. To attain this, two adjacent elements are compared repeatedly and exchanged if they
are not in correct order.
In the first pass, adjacent elements (12,3) will be compared and swapped. The array will look like this
after the first pass:
3, 12, 8, 5
In the second pass, adjacent elements (12,8) will be compared and swapped. The array will look like
this after the second pass:
3, 8, 12, 5
Question 12
An array
18, 13, 2, 9, 5
is
13, 2, 9, 18, 5
after three passes. Which sorting technique is applied on it?
Answer
Explanation
In the first pass, adjacent elements (18, 13) will be compared and swapped. The array will look like
this after the first pass:
13, 18, 2, 9, 5
In the second pass, adjacent elements (18, 2) will be compared and swapped. The array will look like
this after the first pass:
13, 2, 18, 9, 5
In the third pass, adjacent elements (18, 9) will be compared and swapped. The array will look like
this after the third pass:
13, 2, 9, 18, 5
Assignment Questions
Question 1
Answer
An array is a collection of variables of the same type that are referenced by a common name. It is a
reference data type which stores data values in contiguous memory locations. An array is declared
and initialized as follows:
1. Easy to specify — The declaration, allocation of memory space, initialization can all be done
in one line of code.
3. Random access of elements — Arrays facilitate random (direct) access to any element via its
index or subscript.
5. Simple code — As arrays facilitate access of multiple data items of same type through a
common name, the code becomes much simpler and easy to understand.
Question 2
Answer
Each element in an array is referred to by their subscript or index. The index of an element refers to
the position of an element in the array. In Java, the indices start from 0 and go on till size - 1.
For example,
Question 3
Answer
A char data type requires 2 bytes in memory. So, the memory required to store 17 char elements will
be 17 X 2 = 34 bytes.
Question 4
Answer
Question 5
Arrays do not prove to be useful where quite many elements of the same data types need to be
stored and processed. (T/F)
Answer
False
Reason — Arrays store same type of data elements in contiguous memory locations and under a
single variable name. This makes them very useful where quite many elements of the same data
types need to be stored and processed as the user can refer to the elements using the same name
and different index numbers.
Question 6
Answer
True
Question 7
Answer
Single-dimensional arrays are lists of information of the same type and their elements are stored in
contiguous memory locations in their index order.
will have the element grade[0] at the first allocated memory location, grade[1] at the next
contiguous memory location, grade[2] at the next, and so forth. Since grade is a char type array, each
element size is 2 bytes and it will be represented in memory as shown in the figure given below:
Question 8
Determine the number of bytes required to store an int array namely A[23].
Answer
An int data type requires 4 bytes in memory. Therefore, the storage space required by array A[ ] will
be 23 x 4 = 92 bytes.
Question 9
1. a first-in-first-out approach
3. an element name
4. an index number
Answer
an index number
Reason — An array element is accessed using the array name along with an index number, which
corresponds to the position of the element in an array.
Question 10
Answer
Question 11
Write a statement that defines a one-dimensional array called amount of type double that holds two
elements.
Answer
Question 12
Answer
Question 13
1. the eighth
2. the ninth
3. the tenth
4. impossible to tell.
Answer
the tenth
Reason — In Java arrays, element at position 1 has index/subscript 0, element at position 2 has index
1, element at position 3 has index 2 and so on. Thus, position of an element is [index + 1]. Hence,
amount[9] (9 + 1 = 10) will be the tenth element of the array.
Question 14(i)
Answer
Question 14(ii)
Answer
Question 14(iii)
Answer
Question 14(iv)
Answer
Question 15
Declare an array of 5 ints and initialize it to the first five even numbers.
Answer
Question 16
Write a statement that displays the value of the second element in the long array balance.
Answer
System.out.println(balance[1]);
Question 17
For a multidimensional short array X[5][24], find the number of bytes required.
Answer
The formula to calculate total number of bytes required by a two-dimensional array is as follows:
Question 18
Answer
Question 19
Answer
Two-dimensional arrays are stored in a row-column matrix, where the first index indicates the row
and the second indicates the column.
Question 20
What are the preconditions for Binary Search to be performed on a single dimensional array ?
Answer
The preconditions for Binary Search to be performed on a single dimensional array are:
2. Lower bound, upper bound and the sort order of the array must be known.
Question 21(i)
A binary search of an ordered set of elements in an array is always faster than a sequential search of
the elements. True or False ?
Answer
False
Reason — In a case where the search item is at the first place in a sorted array, sequential search
becomes faster than binary search as the first comparison yields the desired value. In case of binary
search, the search value is found after some passes are finished.
For example, let us consider an array arr[] = {2, 5, 8, 12} and the search value = 2. In this case,
sequential search will find the search value in the first comparison while binary search will first
compare the search value with the mid value i.e. 5 and continue its passes.
Question 21(ii)
A binary search of elements in an array requires that the elements be sorted from smallest to largest.
True or False ?
Answer
False
Reason — A binary search requires a sorted array. It may be in ascending order or descending order.
Question 21(iii)
The performance of linear search remains unaffected if an unordered array is sorted in ascending
order. True or False ?
Answer
True
Reason — Linear search is not affected if an unordered array is sorted in ascending order.
Question 21(iv)
For inserting of an element in a sorted array, the elements are always shifted to make room for the
new entrant. True or False ?
Answer
True
Reason — For inserting of an element in a sorted array, the elements are always shifted to make
room for the new entrant, the only exception being when the element is inserted at the end of the
array.
Question 22
Answer
1. Selection sort
2. Bubble sort
3. Shell sort
4. Shuttle sort
5. Quick sort
6. Heap sort
Question 23
Show the contents of the array after the second iteration of Selection Sort.
12071224331141951637358690121722343411519617383596
Answer
In the first iteration, the smallest element 1 will be swapped with the element at first index 12. The
contents of the array after the first iteration of Selection Sort will be:
10712243311419512637358690117223434115196127383596
In the second iteration, the second smallest element 2 will be swapped with the element at second
index 7. The contents of the array after the second iteration of Selection Sort will be:
10217243311419512637358690112273434115196127383596
Question 24
Show the contents of the array after the second iteration of Bubble Sort.
3506182113154957622741865903516283114155967722841965
Answer
In the first iteration, (35,6) will be compared and swapped. The array after the first iteration will look
like:
6035182113154957622741865906135283114155967722841965
In the second iteration, (35,8) will be compared and swapped. After the second iteration, the array
will look like:
6081352113154957622741865906182353114155967722841965
Question 25
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 26
270231321832441512660747859027123233184245161276084795
Which sorting algorithm would produce the following result after three iterations
103152183244275126607478239011325318424527612760847923
Answer
Reason — We can see that after three iterations the first three elements of the array are in their
correct positions. This happens in Selection sort. In Bubble sort, the heaviest element settles at its
appropriate position in the bottom i.e., the array is sorted from the end to the start.
102313218324427512660747859011232331842452761276084795
103123218324427512660747859011322331842452761276084795
103152183244275126607478239011325318424527612760847923
Question 27
130191622335428551616765849013119263243552865171686594
Which sorting algorithm would produce the following result after three iterations.
130612219335428551616765849013162231943552865171686594
Answer
Bubble sort algorithm
Reason — In bubble sort, the adjoining values are compared and exchanged if they are not in proper
order. This process is repeated until the entire array is sorted.
In the first pass, (13,19) will be compared but not swapped. The array after the first pass will be:
130191622335428551616765849013119263243552865171686594
In the second pass, (19, 6) will be compared and swapped. The array after the second pass will be:
130611922335428551616765849013162193243552865171686594
In the third pass, (19, 2) will be compared and swapped. The array after the third pass will be:
130612219335428551616765849013162231943552865171686594
Question 28
Answer
An array is a collection of variables of the same type that are referenced by a common name. It is a
reference data type which stores data values in contiguous memory locations. An array is declared
and initialized as follows:
1. Easy to specify — The declaration, allocation of memory space, initialization can all be done
in one line of code.
3. Random access of elements — Arrays facilitate random (direct) access to any element via its
index or subscript.
5. Simple code — As arrays facilitate access of multiple data items of same type through a
common name, the code becomes much simpler and easy to understand.
Question 29
What are different types of arrays? Give examples of each array type.
Answer
Question 30
Answer
Single-dimensional arrays are lists of information of the same type and their elements are stored in
contiguous memory locations in their index order.
1. A group of contiguous memory locations that all have the same name and same datatype.
will have the element grade[0] at the first allocated memory location, grade[1] at the next
contiguous memory location, grade[2] at the next, and so forth. Since grade is a char type array, each
element size is 2 bytes and it will be represented in memory as shown in the figure given below:
Question 31
How does the amount of storage (in bytes) depend upon type and size of an array? Explain with the
help of an example.
Answer
The amount of storage depends upon the type and size of an array as every data type requires
different storage space in memory and the number of elements (size) of an array determines how
many memory blocks of same size are required.
For example, int arr[] = new int[10]; will require 4 x 10 = 40 bytes space in memory.
Question 32
What do you understand by two-dimensional arrays ? State some situations that can be easily
represented by two-dimensional arrays.
Answer
A two-dimensional array is an array in which each element is itself an array. For instance, an array
A[M][N] is an M by N table with M rows and N columns containing M x N elements.
Some situations that can be easily represented by two-dimensional arrays are as follows:
2. In a business, the sales for a month can easily be represented using a two-dimensional array,
where rows can represent weeks and columns can represent the individual days in a week.
Question 33
Answer
Two-dimensional arrays are stored in a row-column matrix, where the first index indicates the row
and the second indicates the column.
The amount of storage required to hold a two-dimensional array is also dependent upon its base
type, number of rows and number of columns. The formula to calculate total number of bytes
required by a two-dimensional array is given below :
Here, the array pay[][] will require 5 x 7 x 2 = 70 bytes of memory space, as the size of a byte type
element is 2 bytes.
Question 34
import java.util.Scanner;
int m = in.nextInt();
int n = in.nextInt();
A[i] = in.nextInt();
B[i] = in.nextInt();
int idx = 0;
idx++;
int j = 0;
C[idx++] = B[j++];
Output
Question 35
import java.util.Scanner;
A[i][j] = in.nextInt();
System.out.print(A[i][j] + "\t");
System.out.println();
A[i][j] = A[i][j] * 2;
System.out.println("Doubled Array");
for(int i = 0; i < 4; i++)
System.out.print(A[i][j] + "\t");
System.out.println();
Output
Question 36
Let A(n x n) that are not diagonal array. Write a program to find the sum of all the elements which lie
on either diagonal. For example, for the matrix shown below, your program should output 68 = (1 + 6
+ 11 + 16 + 4 + 7 + 10 + 13):
[12345678910111213141516]⎣⎡15913261014371115481216⎦⎤
import java.util.Scanner;
{5, 6, 7, 8} ,
int sum = 0;
if (i == j || (i + j) == 3) {
sum += A[i][j];
Output
Question 37
From a two-dimensional array A[4][4], write a program to prepare a one-dimensional array B[16]
that will have all the elements of A if they are stored in row-major form. For example, for the
following array
[12345678910111213141516]⎣⎡15913261014371115481216⎦⎤
import java.util.Scanner;
{5, 6, 7, 8} ,
int index = 0;
B[index++] = A[i][j];
System.out.println("Array B :");
Output
Question 38
Write a function that checks whether or not two arrays (of characters) are identical, that is, whether
they have same characters and all characters in corresponding positions are equal.
int l1 = A.length;
int l2 = B.length;
if (l1 == l2) {
if(A[i] != B[i]) {
flag = false;
break;
}
else {
flag = false;
if (flag) {
else {
Output
Question 39
Name Marks
. .
. .
. .
Write a program to input the names and marks of the students in the subject. Calculate and display:
(i) The subject average marks (subject average marks = subject total / 50 )
(ii) 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 40
Answer
One-dimensional array stores data in a single Two-dimensional array stores data in a grid or table, with rows an
row or column. columns.
It uses one index to access array elements. It uses two indices to access array elements.
Question 41
Explain
Answer
(i) Linear Search — Linear Search refers to the searching technique in which each element of an array
is compared with the search item, one by one, until the search-item is found or all elements have
been compared.
and the search item 2. Linear search will compare each element of the array to 2 sequentially until
either the search value 2 is found or all the elements have been compared.
(ii) Binary search — Binary Search is a search-technique that works for sorted arrays. Here search-
item is compared with the middle element of array. If the search-item matches with the element,
search finishes. If search-item is less than middle (in ascending array), we perform binary-search in
the first half of the array, other wise we perform binary search in the second half of the array.
and the search item 11. First, the search value will be compared with the middle value 8. Since the
values are not equal, binary search will be performed in the latter half of the array, i.e., {11, 14}. The
search value will be compared with the mid value, which will be 11. Since search value is found,
binary search will stop.
Binary search is more efficient than linear search as it searches the given item in minimum possible
comparisons.
Question 42
Write a program Lower-left-half which takes a two dimensional array A, with size N rows and N
columns as argument and prints the lower left-half.
23150
71531
e.g.,If A is 25781
01501
34915
71
0150
34915
import java.util.Scanner;
int n = in.nextInt();
A[i][j] = in.nextInt();
System.out.println();
}
System.out.println("Lower Left Half array :" );
System.out.println();
Output
Question 43
Answer
import java.util.Scanner;
int i = 0;
X[i] = in.nextInt();
System.out.println();
if (X[i] == item) {
break;
if (i == 10) {
System.out.println("Search Unsuccessful");
else {
System.out.println("Search Successful");
}
}
Output
Question 44
Write a program to search for an ITEM using binary search in array X[10].
import java.util.Scanner;
{
public static void main(String args[]){
X[i] = in.nextInt();
System.out.println();
while (l <= h) {
int m = (l + h) / 2;
l = m + 1;
h = m - 1;
else {
index = m;
break;
}
}
if (index == -1) {
else {
Output
Question 45
The following array of integers is to be arranged in ascending order using the bubble sort technique:
26 21 20 23 29 17
Give the contents of the array at the end of each iteration. Do not write the algorithm.
Answer
The contents of the array at the end of each iteration are as follows:
Question 46
Write a program to search for a given ITEM in a given array X[n] using linear search technique. If the
ITEM is found, move it at the top of the array. If the ITEM is not found, insert it at the end of the
array.
import java.util.Scanner;
int n = in.nextInt();
int B[];
X[i] = in.nextInt();
int i = 0;
// linear search
if (X[i] == item) {
+ i);
break;
if (i == n) {
B[j] = X[j];
B[n] = item;
// item found
else {
X[0] = item;
System.out.println("Array after moving "
Output
Question 47
304172113184295456717878899931096119912031427311418529645771887989109311961299
Make use of binary search technique. Also give the intermediate results while executing this
algorithm.
int h = arr.length - 1;
while (l <= h) {
int m = (l + h) / 2;
System.out.println();
l = m + 1;
h = m - 1;
else {
index = m;
break;
return index;
int X[] = {3, 4, 7, 11, 18, 29, 45, 71, 87, 89, 93, 96, 99};
if (index == -1) {
}
else {
if (index == -1) {
else {
Output
Question 48
1307162213354252866474583951011101317263214355262876484593105111
Write a program to sort the above array using exchange selection sort. Give the array status after
every iteration.
int n = X.length;
int idx = i;
idx = j;
int t = X[i];
X[i] = X[idx];
X[idx] = t;
System.out.println();
System.out.println("Sorted Array:");
}
}
Output
Question 49
For the same array mentioned above in previous question, write a program to sort the above array
using bubble sort technique. Give the array-status after every iteration.
int n = X.length;
//Bubble Sort
int t = X[j];
X[j] = X[j+1];
X[j+1] = t;
System.out.println();
System.out.println("Sorted Array:");
Output
Question 50
Write a program to input integer elements into an array of size 20 and perform the following
operations :
import java.util.Scanner;
System.out.println("Enter 20 numbers:");
arr[i] = in.nextInt();
min = arr[i];
max = arr[i];
sum += arr[i];
}
}
Output
Question 51
Write a program to input forty words in an array. Arrange these words in descending order of
alphabets, using selection sort technique. Print the sorted array.
import java.util.Scanner;
int n = a.length;
a[i] = in.nextLine();
int idx = i;
if (a[j].compareToIgnoreCase(a[idx]) < 0) {
idx = j;
String t = a[idx];
a[idx] = a[i];
a[i] = t;
System.out.println("Sorted Names");
System.out.println(a[i]);
Output
Question 52
Write a program that reads a 4 x 5 two dimensional array and then prints the column sums of the
array along with the array printed in matrix form.
import java.util.Scanner;
public class KboatDDAColSum
arr[i][j] = in.nextInt();
System.out.print(arr[i][j] + "\t");
System.out.println();
int cSum = 0;
cSum += arr[j][i];
System.out.println("Column " + (i + 1)
Output
Question 53
Write a program that inputs a 2D array and displays how the array elements will look in row major
form and column major form.
import java.util.Scanner;
arr[i][j] = in.nextInt();
System.out.print(arr[i][j] + "\t");
System.out.println();
}
}
System.out.println();
Output