Professional Documents
Culture Documents
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
b)
0 0 1
0 1 0
1 0 0
c)
1 0 1
0 1 0
1 0 1
Answer : c
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
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
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.
a) array.length-2-i
b) array.length
c) array.length()
d) i
a) 0
b) array.length
A. j
c) array.length-1
a) n
b) 0
c) j+1
d) 2
a) d == n
b) d == 0
c) temp
d) d == 2
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:
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:
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