Top MCQs on Algorithms in DSA with Answers

Last Updated :
Discuss
Comments

Question 1

Match the following and choose the correct answer for the order A, B, C, D

A. Strassen matrix multiplicationp. Decrease and Conquer
B. Insertion sortq. Dynamic Programming
C. Guassian Eliminationr. Divide and Conquer
D. Floyd shortest path algorithms. Transform and Conquer
  • r, s, p, q

  • r, p, s, q

  • q, s, p, r

  • s, p, q, r

Question 2

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]}

  • {m | m ≀ n, m is prime}

  • Last print never executes

Question 3

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

  • 0

  • n-1

  • n2 - 3n + 2

  • n2 (n+1)/2

Question 4

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 must be 1

  • k must be 0

  • k can take any value between 0 and 1 (d) None of the above

  • None of the above

Question 5

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

  • Leaves s unchanged

  • Reverses all elements of s

  • None of the above

Question 6

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}

Question 7

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

1. Backtracking
2. Greedy method
3. Dynamic programming
4. 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


  • a

  • b

  • c

  • d

Question 8

Let an represent the number of bit strings of length n containing two consecutive 1s. What is the recurrence relation for an?

  • an–2 + an–1 + 2n–2

  • an–2 + 2an–1 + 2n–2

  • 2an–2 + an–1 + 2n–2

  • 2an–2 + 2an–1 + 2n–2

Question 9

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
1. Divide and Conquer
2. Dynamic Programming
3. Greedy design
4. Depth-first search
5. 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
  • a

  • b

  • c

  • d

Question 10

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

  • 0

  • 1

  • 2

  • 3

There are 49 questions to complete.

Take a part in the ongoing discussion