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
1. number[0] is undefined
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];
2. int[ ] y = new int[5];
3. float d[ ] = {1, 2, 3};
4. x = y = new int[10];
5. int a[ ] = {1, 2}; int b[ ]; b = a;
6. int i = 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 :
int x[] = new int[10];
int y[] = new int[10];
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:
int arr[] = new int[10];
The significance of arrays are as follows:
1. Easy to specify — The declaration, allocation of memory space, initialization can all
be done in one line of code.
2. Free from run-time overheads — There is no run-time overhead to allocate/free
memory, apart from once at the start and end.
3. Random access of elements — Arrays facilitate random (direct) access to any
element via its index or subscript.
4. Fast Sequential Access — It is usually faster to sequentially access elements due to
contiguous storage and constant time computation of the address of a component.
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,
int arr[] = {3, 6, 8};
In the array arr[], arr[0] = 3, arr[1] = 6 and arr[2] = 8.
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.
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
2. the dot operator
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
Question 14(i)
Answer
Question 14(ii)
Answer
Question 14(iii)
Declare following arrays: balance of 26 float element.
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
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:
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
Question 21(iii)
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
Answer
Linear search works on sorted and unsorted Binary search works only on sorted arrays (both
arrays. ascending and descending).
Each element of the array is checked against the Array is successively divided into 2 halves and the
target value until the element is found or end of target element is searched either in the first half or in
the array is reached. the 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.
103123218324427512660747859011322331842452761276084795
Array after the third iteration:
103152183244275126607478239011325318424527612760847923
Question 27
130191622335428551616765849013119263243552865171686594
Which sorting algorithm would produce the following result after three iterations.
130612219335428551616765849013162231943552865171686594
Answer
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:
int arr[] = new int[10];
The use of arrays have the following benefits:
1. Easy to specify — The declaration, allocation of memory space, initialization can all
be done in one line of code.
2. Free from run-time overheads — There is no run-time overhead to allocate/free
memory, apart from once at the start and end.
3. Random access of elements — Arrays facilitate random (direct) access to any
element via its index or subscript.
4. Fast Sequential Access — It is usually faster to sequentially access elements due to
contiguous storage and constant time computation of the address of a component.
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.
2. A reference that stores the beginning address of the array elements.
3. A separate instance variable containing the number of elements in the array.
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.
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.
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 :
total bytes = number-of-rows x number-of-columns x size-of-base-type
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;
public class KboatArrayMerge
{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter size m : ");
int m = in.nextInt();
int A[] = new int[m];
int idx = 0;
while(idx < A.length) {
C[idx] = A[idx];
idx++;
}
int j = 0;
while(j < B.length) {
C[idx++] = B[j++];
}
Output
Question 35
import java.util.Scanner;
System.out.println("Doubled Array");
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
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;
int sum = 0;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if (i == j || (i + j) == 3) {
sum += A[i][j];
}
}
}
}
}
Output
Question 37
[12345678910111213141516]⎣⎡15913261014371115481216⎦⎤
the resultant array should be : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import java.util.Scanner;
System.out.println("Array B :");
for(int i = 0; i < 16; i++)
{
System.out.print(B[i] + " ");
}
}
}
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.
if (flag) {
System.out.println("Arrays are equal");
}
else {
System.out.println("Arrays are not equal");
}
}
}
Output
Question 39
Name Marks
. .
. .
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;
Question 40
Answer
One-dimensional array stores data in a Two-dimensional array stores data in a grid or table, with
single row or column. rows and columns.
One-dimensional array Two-dimensional array
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.
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.
2 3 1 5 0
7 1 5 3 1
e.g.,If A is 2 5 7 8 1
0 1 5 0 1
3 4 9 1 5
2
7 1
The output will be 2 5 7
0 1 5 0
3 4 9 1 5
import java.util.Scanner;
System.out.println();
}
}
}
Output
Question 43
import java.util.Scanner;
if (i == 10) {
System.out.println("Search Unsuccessful");
}
else {
System.out.println("Search Successful");
System.out.println(item + " is present at index "
+ i);
}
}
}
Output
Question 44
Write a program to search for an ITEM using binary search in array X[10].
import java.util.Scanner;
if (index == -1) {
System.out.println("Search item not found");
}
else {
System.out.println(item + " found at index " +
index);
}
}
}
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 i = 0;
// linear search
for (i = 0; i < n; i++) {
if (X[i] == item) {
System.out.println("Search item found at index
"
+ i);
break;
}
}
Output
Question 47
304172113184295456717878899931096119912031427311418529645771887
989109311961299
Make use of binary search technique. Also give the intermediate results while executing this
algorithm.
System.out.println();
int X[] = {3, 4, 7, 11, 18, 29, 45, 71, 87, 89, 93,
96, 99};
}
}
Output
Question 48
int t = X[i];
X[i] = X[idx];
X[idx] = t;
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(X[i] + " ");
}
}
}
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.
//Bubble Sort
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (X[j] > X[j + 1]) {
int t = X[j];
X[j] = X[j+1];
X[j+1] = t;
}
}
System.out.println("Pass :" + (i + 1));
for(int k = 0; k < n; k++) {
System.out.print(X[k] + " ");
}
System.out.println();
}
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(X[i] + " ");
}
}
}
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;
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;
System.out.println("Sorted Names");
for (int i = 0; i < n; i++) {
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
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
}
}
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;
System.out.println();
}
}
Output