MCQ

Algorithms MCQ – Data Structures & Complexity – Part 1

Computer architecture MCQ questions and answers for the preparation of tests, exams, and certifications. So you will find questions about loops and conditionals, data structure, complexity, flowchart, pseudocode, and much more. This systematic learning method will easily prepare anyone to pass their exam.
 

1. The timing factor to determine the efficiency of the algorithm is measured by _____?

A Counting microseconds

B Count the number of operations

C Counting the number of declarations

D Counting the kilobytes of the algorithm.
 

 
B

 

 

2. What is the time complexity of the following code?
int count(int n)
{
  int c = 0;
  for (int i = 0; i < n; i++)
     for (int j = i; j > 0; j--)
        c = c + 1;
  return c;
}

A O(n)

B O(n^2)

C O(n*Logn)

D O(n*Logn*Logn)

B
The time complexity can be calculated by counting the number of times the expression “c = c + 1;” is executed. The expression is executed 0 + 1 + 2 + 3 + 4 + …. + (n-1) times.

Time complexity = O(0 + 1 + 2 + 3 + .. + n-1) = O(n * (n-1) / 2) = O(n ^ 2)

 

 

3. Which of the following data structures is not a linear data structure?

A Array

B Linked list

C Both A and B are true.

D None of the above

D
 

4. What is the time complexity of the following code?
int count(int n)
{
   int c = 0;
   for(int i = n; i > 0; i/= 2)
      for(int j = 0; j < i; j++)
         c += 1;
   return c;
}

A O(n^2)

B O(n*Logn)

C O(n)

D O(n*Logn*Logn)
 

 
C
As “nbr” is an integer input, the function count() is executed as follows: n + n / 2 + n / 4 + … 1
So, the time complexity T(n) can be written as a sequence:
T (n) = O (n + n / 2 + n / 4 + … 1) = O (n)
The value of c is also :
n + n / 2 + n / 4 + .. + 1

 

 

5. The recurrence relation capturing the optimal time of the Tower of Hanoi problem with n disks is _______?

A T (n) = 2T (n – 2) + 2

B T (n) = 2T (n – 1) + n

C T (n) = 2T (n / 2) + 1

D T (n) = 2T (n – 1) + 1

D

 
Here are the steps to follow to solve the Tower of Hanoi problem recursively.

Consider the three pegs A, B and C. The goal is to move n discs from A to C.
To move n discs from peg A to peg C:

  • move the n-1 disks from A to B. This leaves disc n alone on peg A
  • move the disk n from A to C
  • move n?1 disks from B to C so that they are on disk n

The recurrence function T (n) for the time complexity of the above recursive solution can be written as follows: T (n) = 2T (n-1) + 1

 

 

6. The complexity of the Linear Search algorithm is _____?

A O(n)

B O(log n)

C O(n2)

D O(n * log n)

A

 

 

7. The average complexity occurs in the Linear Search algorithm. When the searched element _______?

A Is in the middle of the array

B Not found in the array

C Is the last element of the array

D Is the last element of the array or not at all

A

 

 

8. What is the worst-case recurrence of the quick sort algorithm and what is the worst-case time complexity?

A The recurrence is T (n) = T (n-2) + O (n) and the time complexity is O (n ^ 2)

B The recurrence is T (n) = T (n-1) + O (n) and the time complexity is O (n ^ 2)

C The recurrence is T (n) = 2T (n / 2) + O (n) and the time complexity is O (n*Logn)

D The recurrence is T (n) = T (n / 10) + T (9n / 10) + O (n) and the time complexity is O (n*Logn)
 

 
B
The worst case of the quick sort algorithm occurs when the selected key element is at the end of the array. In the worst case, the quick sort algorithm recursively calls a subproblem of size 0 and another subproblem of size (n-1). The recurrence is therefore T (n) = T (n-1) + T (0) + O (n) The expression can be rewritten as T (n) = T (n-1) + O (n)

 

 

9. Which of the following data structures is a linear data structure?

A Tree

B Graphs

C Arrays

D None of the above

C
 
10. Suppose we have an O(n) time algorithm that finds the median of an unsorted array. Now consider an implementation of the quick sort algorithm where we first find the median using the quick sort algorithm, and then use the median as a pivot. What will be the worst case time complexity of this modified algorithm?
 
A O(n^2 Logn)

B O(n^2)

C O(n*Logn*Logn)

D O(n*Logn)

D
If we use the median as the pivot element, then the recurrence in all cases becomes T (n) = 2T (n / 2) + O (n).

 

 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *