Top MCQs on Algorithms in DSA with Answers (original) (raw)
Match the following and choose the correct answer for the order A, B, C, D
| A. Strassen matrix multiplication | p. Decrease and Conquer |
|---|---|
| B. Insertion sort | q. Dynamic Programming |
| C. Guassian Elimination | r. Divide and Conquer |
| D. Floyd shortest path algorithm | s. Transform and Conquer |
In the following C program fragment, j, k n and TwoLog_n are integer variables, and A is an array of integers. The variable n is initialized to an integer ≥ 3, and TwoLog_n is initialized to the value of 2*⌈log2(n)⌉
C `
for (k = 3; k < = n; k++) A[k] = 0; for (k = 2; k < = TwoLog_n; k++) for (j = k + 1; j < = n; j++) A[j] = A[j] || (j % k); for (j = 3; j < = n; j++) if (!A[j]) printf("%d", j);
`
The set of numbers printed by this program fragment is
- {m | m ≤ n, (∃ i) [m = i!]} Here i! mean factorial of i
- {m | m ≤ n, (∃ i) [m = i2]}
- Last print never executes
An n x n array v is defined as follows:
v[i, j] = i-j for all i, j, 1 <= i <= n, 1 <= j <= n
The sum of the elements of the array v is
X, Y and Z are closed intervals of unit length on the real line. The overlap of X and Y is half a unit. The overlap of Y and Z is also half a unit. Let the overlap of X and Z be k units. Which of the following is true?
- k can take any value between 0 and 1 (d) None of the above
Suppose you are given an array s[1..n] and a procedure reverse (s, i, j) which reverses the order of elements in a between positions i and j (both inclusive). What does the following sequence do, where 1 <= k <= n:
reverse(s, 1, k) ;
reverse(s, k + 1, n);
reverse(s, l, n);
- Rotates s left by k positions
- Reverses all elements of s
Let swap() be a function that swaps two elements using their addresses. Consider the following C function.
C `
void fun(int arr[], int n) { for (int i = 0; i < n; i+=2) { if (i>0 && arr[i-1] > arr[i] ) swap(&arr[i], &arr[i-1]); if (i<n-1 && arr[i] < arr[i+1] ) swap(&arr[i], &arr[i + 1]); } }
`
If an array {10, 20, 30, 40, 50, 60, 70, 80} is passed to the function, the array is changed to
- {20, 10, 40, 30, 60, 50, 80, 70}
- {10, 30, 20, 40, 60, 50, 80, 70}
- {10, 20, 30, 40, 50, 60, 70, 80}
- {80, 70, 60, 50, 40, 30, 20, 10}
Match the following
List-I
A. Prim’s algorithm for minimum spanning tree
B. Floyd-Warshall algorithm for all pairs shortest paths
C. Mergesort
D. Hamiltonian circuit
List-II
- Backtracking
- Greedy method
- Dynamic programming
- Divide and conquer
Codes: A B C D
(a) 3 2 4 1
(b) 1 2 4 3
(c) 2 3 4 1
(d) 2 1 3 4
Let an represent the number of bit strings of length n containing two consecutive 1s. What is the recurrence relation for an?
Given below are some algorithms, and some algorithm design paradigms.
List-I
A. Dijkstra’s Shortest Path
B. Floyd-Warshall algorithm to compute all pairs shortest path
C. Binary search on a sorted array
D. Backtracking search on a graph
List-II
- Divide and Conquer
- Dynamic Programming
- Greedy design
- Depth-first search
- Breadth-first search
Match the above algorithms on the left to the corresponding design paradigm they follow Codes:
A B C D (a) 1 3 1 5
(b) 3 3 1 5
(c) 3 2 1 4
(d) 3 2 1 5
Suppose c = 〈c[0], ... , c[k – 1]〉 is an array of length k, where all the entries are from the set {0, 1}. For any positive integers a and n, consider the following pseudocode.
DOSOMETHING (c, a, n)
z ← 1
for i ← 0 to k – 1
do z ← z2 mod n
if c[i] = 1
then z ← (z × a) mod n
return z
If k = 4, c = 〈1, 0, 1, 1〉, a = 2 and n = 8, then the output of DOSOMETHING(c, a, n) is ____________.
There are 49 questions to complete.
Take a part in the ongoing discussion