logo
Data Structures Algorithms - Quiz(MCQ)
1 .
What will be the output of the following code snippet?
void solve() {
   int a[] = {1, 2, 3, 4, 5};
   int sum = 0;
   for(int i = 0; i < 5; i++) {
       if(i % 2 == 0) {
           sum += a[i];
       }
   }
   cout << sum << endl;
}
A)
15
B)
9
C)
6
D)
5

Correct Answer : Option (B) :   9

2 .
What will the output of the following code snippet?
void solve() {
   int a[] = {1, 2, 3, 4, 5};
   int sum = 0;
   for(int i = 0; i < 5; i++) {
       if(i % 2 == 0) {
           sum += *(a + i);
       }
       else {
           sum -= *(a + i);
       }
   }
   cout << sum << endl;
}
A)
2
B)
3
C)
15
D)
Syntax Error

Correct Answer : Option (B) :   3


Explaination : This is an example of accessing an array element through pointer notation. So *(a + i) is equivalent to a[i], and we are adding all the even indices elements, and subtracting all the elements at odd indices, which gives a result of 3.

A)
Elements are stored in contiguous memory blocks.
B)
Elements of an array can be accessed in constant time.
C)
Multiple other data structures can be implemented using arrays.
D)
The amount of memory to be allocated should be known beforehand.

Correct Answer : Option (D) :   The amount of memory to be allocated should be known beforehand.

A)
LinkedList of characters.
B)
An array of characters.
C)
The object of some class.
D)
Same as other primitive data types.

Correct Answer : Option (B) :   An array of characters.

5 .
What will be the value of “sum” after the following code snippet terminates?
void solve(ListNode* root) {
   /*
   The LinkedList is defined as:
   root-> val = value of the node
   root-> next = address of next element from the node 
   The List is 1 -> 2 -> 3 -> 4 -> 5
   */
   int sum = 0;
   while(root -> next != NULL) {
       sum += root -> val;
       root = root -> next;
   }
   cout << sum << endl;
}
A)
1
B)
2
C)
10
D)
20

Correct Answer : Option (C) :   10


Explaination : The code calculates the sum of values of the nodes of the given LinkedList, which is 10.

6 .
What will be the output of the following code snippet?
int search(int l, int r, int target, vector<int> &a) {
   int mid = (l + r) / 2;
   if(a[mid] == target) {
       return mid;
   }
   else if(a[mid] < target) {
       return search(mid + 1, r, target, a);
   }
   else {
       return search(0, mid - 1, target, a);
   }
}
void solve() {
   vector<int> a = {1, 2, 3, 4, 5};
   cout << search(0, 4, 4, a) << endl;
}
A)
4
B)
3
C)
2
D)

Correct Answer : Option (B) :   3


Explaination : The above code is just a recursive implementation of binary search which searches for the index of value 4, which is 3.

7 .
What will be the output of the following code snippet?
void solve() {
   int n = 24;
   int l = 0, r = 100, ans = n;
   while(l <= r) {
       int mid = (l + r) / 2;
       if(mid * mid <= n) {
           ans = mid;
           l = mid + 1;
       }
       else {
           r = mid - 1;
       }
   }
   cout << ans << endl;
}
A)
1
B)
2
C)
3
D)
4

Correct Answer : Option (D) :   4


Explaination : The code snippet basically uses binary search to calculate the floor of the square root of a number. Since the square root is an increasing function, so binary search is applicable here. Here, for n = 24, the answer is 4.

A)
AVL Trees.
B)
Hash Tables.
C)
Red-Black Trees.
D)
Binary Search Trees.

Correct Answer : Option (C) :   Red-Black Trees.

9 .
What will be the output of the following code snippet?
void solve() {
   string s = "00000001111111";
   int l = 0, r = s.size() - 1, ans = -1;
   while(l <= r) {
       int mid = (l + r) / 2;
       if(s[mid] == '1') {
           ans = mid;
           r = mid - 1;
       }
       else {
           l = mid + 1;
       }
   }
   cout << ans << endl;
}
A)
7
B)
6
C)
5
D)
4

Correct Answer : Option (A) :   This code snippet shows one of the many applications of binary search. Here, we are binary searching for the index of the first occurrence of the character ‘1’ in the given string. When we get the character ‘1’ at the mid index, we store it as the answer and move to the left half which will have the first index of ‘1’ if it occurs. Else we move to the right half. So, the answer will be 7 (0-based indexing).

10 .
What is the output of the following code snippet?
void solve() {
   stack<int> s;
   s.push(1);
   s.push(2);
   s.push(3);
   for(int i = 1; i <= 3; i++) {
       cout << s.top() << “ “;
       s.pop();
   }
}
A)
1
B)
2
C)
1 2 3
D)
3 2 1

Correct Answer : Option (D) :   3 2 1


Explaination : Stack follows a last in first out methodology, so the elements are popped out in reverse order.