Array and Array Operations – 2

Subject:  Data Structure

Topic: Array and Array Operations

Difficulty Level: Difficult

No. of Questions: 10

11. What is the time complexity of finding the maximum element in an unsorted array?

a) O(1)

b) O(log n)

c) O(n)

d) O(n^2)

View Answer

Answer: c) O(n)

Answer Explanation: Finding the maximum element in an unsorted array requires checking each element, resulting in a time complexity of O(n).

12. How does the time complexity change when inserting an element at the middle of a dynamic array that requires resizing?

a) O(1)

b) O(n)

c) O(n log n)

d) O(n^2)

View Answer

Answer: b) O(n)

Answer Explanation: Inserting an element in the middle requires shifting elements to the right. If resizing is needed, the overall complexity remains O(n).

13. What is the output of the following code snippet?

int arr[] = {4, 7, 1, 3, 9, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for(int i = 0; i < n; i += 2) {
  sum += arr[i];
}
printf("%d", sum);

a) 10

b) 14

c) 16

d) 20

View Answer

Answer: c) 16

Answer Explanation: The sum of elements at even indices (0, 2, 4) is 4 + 1 + 9 = 14.

14. Which sorting algorithm is most efficient for sorting a small array and why?

a) Quick sort

b) Merge sort

c) Insertion sort

d) Bubble sort

View Answer

Answer: c) Insertion sort

Answer Explanation: Insertion sort is efficient for small arrays because it has a low overhead and performs well on nearly sorted data.

15. Given the array int arr[] = {5, 3, 6, 2, 10}, which of the following represents the array after performing one iteration of the selection sort algorithm?

a) {2, 3, 6, 5, 10}

b) {3, 5, 6, 2, 10}

c) {5, 6, 3, 2, 10}

d) {2, 5, 6, 3, 10}

View Answer

Answer: d) {2, 5, 6, 3, 10}

Answer Explanation: The smallest element (2) is swapped with the first element (5).

16. How do you reverse an array in-place in C?

a) Using a temporary array

b) Using the standard library function

c) Swapping elements from ends towards the middle

d) By sorting the array in descending order

View Answer

Answer: c) Swapping elements from ends towards the middle

Answer Explanation: To reverse an array in-place, swap the elements from the two ends towards the middle.

17. What is the best method to find a missing number in an array containing integers from 1 to n with one missing number?

a) Sorting the array

b) Using a hash table

c) Summing the first n natural numbers and subtracting the array sum

d) Using binary search

View Answer

Answer: c) Summing the first n natural numbers and subtracting the array sum

Answer Explanation: Sum the first n natural numbers using the formula n*(n+1)/2 and subtract the sum of the array elements to find the missing number.

18. Given an array with the following elements: {15, 22, 13, 27, 12}, what will be the index of the pivot after the first partitioning step in the quicksort algorithm?

a) 0

b) 2

c) 3

d) 4

View Answer

Answer: b) 2

Answer Explanation: After the first partitioning step, the pivot (12) will be placed at index 2, with all smaller elements to the left and larger to the right.

19. Which of the following algorithms can be used to find the kth smallest element in an unsorted array?

a) Merge sort

b) Quick select

c) Bubble sort

d) Binary search

View Answer

Answer: b) Quick select

Answer Explanation: Quick select is a selection algorithm to find the kth smallest element in an unordered list, and it is related to the quicksort algorithm.

20. What is the worst-case time complexity of the merge sort algorithm?

a) O(n log n)

b) O(n^2)

c) O(log n)

d) O(n)

View Answer

Answer: a) O(n log n)

Answer Explanation: The worst-case time complexity of the merge sort algorithm is O(n log n) due to the divide and conquer approach.

This set of Data Structure question and answers contain 10 difficult MCQs from Array and Array Operations for practice.