Chapter 3
Arrays
Tick the correct answer
Question 1
Which of the following is the correct usage?
1. int a[-40]
2. int a[40] ✓
3. float a[0 - 40]
4. None
Question 2
Which element is represented by a[10]?
1. 10th
2. 9th
3. 11th ✓
4. None
Question 3
Cell numbers of a dimensional array are also known as:
1. packets
2. blocks
3. subscripts ✓
4. compartments
Question 4
A dimensional array is also known as:
1. subscripted variable ✓
2. actual variable
3. compound variable
4. none
Question 5
An array element can be accessed through:
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
The following statement :
int code[ ]= {25,37,38,42};
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
The elements of array[50] are numbered:
1. from 1 to 50
2. from 0 to 49 ✓
3. from 1 to 51
4. none
Question 9
Which of the following function finds the size of array
char m[] = {'R', 'A', 'J', 'E', 'N', 'D', 'R', 'A' };?
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
Give the output of the following
Question 1
int m[] = {2,4,6,8};
System.out.println(m[1] + " " + m[2]);
Output
4 6
Explanation
m[1] gives the second element of the array which is 4
m[2] gives the third element of the array which is 6
Question 2
int a[] ={2,4,6,8,10};
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:
a[0]+a[1] ⇒ 23 + 4 ⇒ 27
{23, 4, 6, 4, 10}
Question 3
int a[]=new int [5];
a[0]=4; a[1]=8; a[2]=7; a[3]=12; a[4]=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
⇒2+8
a[0] + a[3]
⇒10
0 First Iteration
⇒4+6
a[1] + a[2]
⇒10
1 Second Iteration
Write short answers(Write in Class Work)
Question 1
What is meant by Dimensional Array?
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
Name the two types of Dimensional Array.
1. Single Dimensional Array
2. Double Dimensional Array
Question 3
What is the need of Dimensional Array? Explain.
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
'Array is a composite data type'. Explain this statement.
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
Define the following with their constructs:
(a) Single Dimensional Array
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>];
Differentiate between the following((Write in Class Work)
Question 1
Subscript and Subscripted variable
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] and int a[5]
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
Ordinary variable and array variable
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 and Searching
Sorting Searching
Sorting means to arrange the elements of the Searching means to search for a term or
array in ascending or descending order. value in an array.
Bubble sort and Selection sort are examples of Linear search and Binary search are
sorting techniques. examples of search techniques.
Selection sort and Bubble sort
Question 7
length and length()
length length()
length() is a member method of
length is an attribute i.e. a data member of array.
String class.
It gives the length of an array i.e. the number of It gives the number of characters
elements stored in an array. present in a string.