You are on page 1of 5

Predict the output:

Question 1

int m[] = {2,4,6,8};


System.out.println(m[1] + " " + m[2]);

Output

46

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:
{23, 4, 6, 4, 10}
a[0]+a[1] ⇒ 23 + 4 ⇒ 27

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

a[0] + a[3]
0 ⇒2+8 First Iteration
⇒10

a[1] + a[2]
1 ⇒4+6 Second Iteration
⇒10

Write short answers

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>];
(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>[][] = new <type>[<rows>][<columns>];
OR
<type> [][] <array-variable> = new <type>[<rows>][<columns>];
6. Linear search and Binary search

Linear Search Binary Search

Linear search works on sorted and Binary search works on only sorted arrays
unsorted arrays (ascending or descending)

Each element of the array is checked


Array is successively divided into 2 halves
against the target value until the
and the target element is searched either in
element is found or end of the array is
the first half or in the second half
reached

Linear Search is slower Binary Search is faster


8. 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.

You might also like