You are on page 1of 11

Arrays

1. What does the following piece of code do?


for(int i =0; i < arr.length-1; i++)
{
for(int j = i + 1; j < arr.length; j++)
{
if((arr[i] == (arr[j])) && (i != j))
{
System.out.println(arr[i]);
}
}
}
a) Print the duplicate elements in the array
b) Print the element with maximum frequency
c) Print the unique elements in the array
d) Prints the element with minimum frequency
Answer: a
Explanation: The print statement is executed only when the items are equal and their indices are
not.

2. Given an array arr = {5,6,77,88,99} and key = 88; How many iterations are done until the element is
found? Binary search
a) 1
b) 3
c) 4
d) 2
Answer: d
Explanation: Iteration1 : mid = 77; Iteration2 : mid = 88;

3.
boolean swapped = true;
for(int j = arr.length-1; j >= 0 && swapped; j--)
{
swapped = false;
for(int k = 0; k < j; k++)
{
if(arr[k] > arr[k + 1])
{
int temp = arr[k];
arr[k] = arr[k + 1];
arr[k + 1] = temp;
swapped = true;
}
}
}

4. The given array is arr = {1, 2, 4, 3}. Bubble sort is used to sort the array elements. How many
iterations will be done to sort the array using the above code for sorting?
a) 4
b) 2
c) 1
d) 0
Answer: a
Explanation: Even though the first two elements are already sorted, bubble sort needs 4 iterations to
sort the given array.
5. Consider a situation where swap operation is very costly. Which of the following sorting
algorithms should be preferred so that the number of swap operations are minimized in
general?

a) Heap Sort
b) Selection Sort
c) Insertion Sort
d) Merge Sort

Answer: (B)
Explanation: Selection sort makes O(n) swaps which is minimum among all sorting algorithms
mentioned above.

6. What are the correct intermediate steps of the following data set when it is being sorted with the
Selection sort? 15,20,10,18
a) 10, 20,15,18 -- 10,15,20,18 -- 10,15,18,20
b) 15,20,10,18 -- 15,10,20,18 -- 10,15,20,18 -- 10,15,18,20
c) 15,18,10,20 -- 10,18,15,20 -- 10,15,18,20 -- 10,15,18,20
d) 15,10,20,18 -- 15,10,18,20 -- 10,15,18,20
Answer :- A

7. Which of the following sorting algorithms in its typical implementation gives best performance
when applied on an array which is sorted or almost sorted (maximum 1 or two elements are
misplaced).
a) Quick Sort
b) Bubble
c) Selection Sort
d) Insertion Sort
Answer: (D)

Explanation: Insertion sort takes linear time when input array is sorted or almost sorted (maximum
1 or 2 elements are misplaced).
All other sorting algorithms mentioned above will take more than linear time in their typical
implementation.

8. Find the error in the given code to sort array in ascending order
voidinsertionSort(int arr[], int n)
{
    int i, key, j;
for(i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
  /* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while(arr[j] > key && j >= 0)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
a) Array is sorted in descending order
b) Infinite loop
c) No sorting will take place
d) ArrayIndexOutOfBoundException
Answer:- ArrayIndexOutOfBoundException

9. Specify Output of given program snippet


int i, j;
int a[][]={{0,0,0},{0,0,0},{0,0,0}};
for(i = 0; i < 3; i++)
{
for(j = 0;j < 3; j++)
if(i == j || i + j == 2)
a[i][j] = 1;
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
a)
1 0 0
0 1 0
0 0 1

b)
0 0 1
0 1 0
1 0 0

c)
1 0 1
0 1 0
1 0 1

Answer : c

10. Specify the operation performed by then given program snippet


for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
a) Normal of two matrix
b) Trace of two matrix
c) Multiplication of two matrix
d) Transpose of two matrix

Answer: Multiplication of two matrix

11. Specify the output of given code.

int i, t;
int A[] = {1, 2, 3, 4, 5, 6};
int size = A.length;
for(i = 0; i < 6; i++)
{
t = A[i];
A[i] = A[size – i - 1];
A[size – i - 1] = t;
}
for(i = 0; i < size; i++)
System.out.print(A[i]+ " ");

a) 1 2 3 4 5 6
b) 6 5 4 3 2 1
c) 1 3 5 2 4 6
d) 2 4 6 1 3 5

CORRECT ANSWER :- A

12. Specify the output of the given function


static void display()
{
int i,j;
int A[]={1,0,0,0,0,0,0};
for(i=0;i<6;i++)
{
for(j=0;j<=i;j++)
System.out.print(A[j]+" ");
for(j=i+1;j>0;j--)
A[j]=A[j]+A[j-1];
System.out.println();
}
}
A:-
1
11
121
1331
14641
1 5 10 10 5 1
B.
1
10
100
1000
10000
100000
C.
1
11
111
1111
11111
111111

ANSWER :- A

13.

?1? ar[0][0]
?2? r-1
?3? j == 0
?4? hg < ar[i][j]
?5? is > ar[i][j]

14.
Answers:
?1? is array.length
?2? is array[j]
?3? is i> -1
?4? is i - -
?5? is i + 1

15.

a) 4
b) 2
c) 3.5
d) 8

16. A matrix B[10][20] is stored in the memory with each element requiring 2 Bytes of storage. If the base
address at B[2][1] is 2140, find the address of B[5][4] when the matrix is stored in Column Major
Wise.
a) 2026
b) 2208
c) 2260
d) 2206

17. AN ARRAY ELEMENT CAN BE ACCESSED THROUGH


a) DOT(.)
b) COMMAS
c) INDEX NUMBER
d) NONE

18. What is the base type of the Array


a) Datatype
b) Dimension type
c) type casting
d) int type

19. What is out of bound subscript error?


a) when accessing an array element the subscript is written out of square brackets
b) when accessing an array element the subscript is written without using square brackets
c) when an invalid subscript is used while accessing an array element
d) when an invalid array name is used while accessing an array element

20. Valid range of subscript for an array declared of size n is:


a) 1 to n
b) 1 to n - 1
c) 0 to n - 1
d) 0 to n
21. Given the code to sort an array of 5 elements in ascending order using insertion Sort technique.
Select the Correct option for the blanks.
Import java.util.*;
class insertion
{
public void main()
{
Scanner sc=new Scanner(System.in);
int a[] = new int[5];
System.out.println("enter 5 INTEGERS");
int i;
for(i = 0; i < 5; i++)
{
a[i] = sc.nextInt();
}

System.out.println("ARRAY ELEMENTS");
for(i = 0; i < 5; i++)
{
System.out.print( (A) +"\t");
}
for(i = 1;i < 5; i++)
{
int j = (B)_________
int k = a[i];
while(( ( C ) ) && (j>=0))
{
______(D)______=a[j];
j = j - 1;
}
( E)______ = k;
}
System.out.println();
System.out.println("ARRAY ELEMENTS after sorting");
for(i = 0; i < 5; i++)
{
System.out.print( a[i]+"\t");
}
}
}
20 (A)
a) a[--i]
b) a[i]
c) a[i+1]
d) a[i-1]

20 (B)
a) --I
b) ++I
c) i-1
d) i+1

20 (C)
a) k >= a[j]
b) k < a[j]
c) k > a[j+1]
d) k < a[j+1]

20 (D)
a) a[j-1]
b) a[++j]
c) a[j++]
d) a[j+1]

20 (E)
a) a[j]
b) a[j]
c) a[j+1]
d) a[j++]

22. A matrix ARR[-4…6,3…8] is stored in the memory with each element requiring 4 bytes of storage. If
the base address is 1430, find the address of ARR[3][6] when the matrix is stored in Row Major Wise.
a) 1610
b) 1600
c) 1630
d) 1580

23. A matrix A[m][m] is stored in the memory with each element requiring 4 bytes of storage. If the base
address at A[1][1] is 1500 and the address of A[4][5] is 1608, determine the order of the matrix when
it is stored in Column Major Wise.
a) 4
b) 2
c) 3
d) 6

24. The following is a function of some class which checks if a positive integer is an Palindrome number
by returning true or false. (A number is said to be palindrome number if the reverse of the number is
equal to the original number). The function does not use modulus (%) operator to extract digits.
There are some places in the code marked by ?1?,?2?,?3?,?4?,?5? which may be replaced by a
statement/expression so that the function.

boolean PalindromeNum(int N)
{
int rev = ?1?;
int num = N;
while(num > 0)
{
int f = num / 10;
int s = ?2?;
int digit = num - ?3?;
rev = ?4? + digit;
num /= ?5?;
}
if(rev == N)
return true;
else return false;
}
?1?
a) (i)1
b) 0
c) N
d) num

?2?
a) f*10
b) f/10
c) 1
d) f

?3?
a) s
b) f
c) num/10
d) N

?4?
a) num
b) rev*10
c) digit*10
d) s

?5?
a) 10
b) num/10
c) ++num
d) Num * 10

25. Each element of an array arr[-5….15,-10…15] requires ‘W’ bytes of storage. If the address of arr[6][8]
is 4778 and the Base Address at arr[1][1] is 4000, find the width ‘W’ of each cell in the array arr[][]
when the array is stored as Column Major Wise.
a) 4
b) 2
c) 8
d) None of there
Correct answer : b

26. A square matrix A[m x m] is stored in the memory with each element requiring 2 bytes of storage. If
the base address A[1][1] is 1098 and the address at A[4][5] is 1144, determine the order of the matrix
A[m x m] when the matrix is stored column majorwise.

a) 4X4
b) 2X2
c) 8X8
d) 3
27. The following program code sorts a single dimensional array in ascending order using Bubble Sort
technique. There are some places in the code marked as ?1?, ?2?, ?3?, ?4? and ?5? which are to be
replaced by a statement/expression so that the code works properly.

void BubbleSort(int array[])


{
for(i = 0;i <= array.length - 2; i++)
{
for (int j = 0; j <?1?;j++)
{
if(array[?2?] > array[?3?])
{
?4?=array[j];
array[j]=?5?;
array[j+1]=temp;
}
}
}
}

26 (A) Select the correct option for ?1?:

a) array.length-2-i
b) array.length
c) array.length()
d) i

26 (B) Select the correct option for ?2?:

a) 0
b) array.length
A. j
c) array.length-1

26 (C) Select the correct option for ?3?:

a) n
b) 0
c) j+1
d) 2

26 (D) Select the correct option for ?4?:

a) d == n
b) d == 0
c) temp
d) d == 2

26 (E) Select the correct option for ?5?:

a) array[j + 1]
b) if else
c) for
d) switch
28. For displaying mirror image of a 2-D array of size mXn, the following statement is true:

a) the column loop will execute from n-1 to 0


b) the column loop will execute from 0 to n-1
c) the row loop will execute from 0 to m-1
d) the row loop will execute from m-1 to 0

29. The total number of bytes occupied by a 2-D array arr[3][4] of boolean data type will be:

a) 12 Bytes
b) 1 Byte
c) 2 Bytes
d) Infinite

30. The address of the elements on the main diagonal of a 2-D array of size mXm will have:

a) Same column number and Row number


b) Column number > Row Number
c) Column Number < Row Number
d) Column number =0 and row number = m - 1

31. The following run time error is observed if we declare an array but donot create it with new keyword:

a) NullPointerException
b) ArrayIndexOutOfBoundsException
c) NumberFormatException
d) FileNotFoundException

32. A Matrix MAT[10][5] is stored in the memory in Row Major Wise with each element requiring 2
bytes of storage. If the base address at MAT[0][0] is 1250, then the address of MAT[5][6] will
be:

a) 1320
b) 1312
c) 2013
d) None of the above

You might also like