This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Search an Element in an Array using Recursion – 1”.
1. Which of the following techniques can be used to search an element in an unsorted array?
a) Iterative linear search
b) Recursive binary search
c) Iterative binary search
d) Normal binary search
View Answer
Explanation: Iterative linear search can be used to search an element in an unsorted array.
Note: Binary search can be used only when the array is sorted.
2. Consider the array {1,1,1,1,1}. Select the wrong option?
a) Iterative linear search can be used to search for the elements in the given array
b) Recursive linear search can be used to search for the elements in the given array
c) Recursive binary search can be used to search for the elements in the given array
d) No method is defined to search for an element in the given array
View Answer
Explanation: Iterative linear search, Recursive linear search and Recursive binary search can be applied to search for an element in the above given array.
3. What does the following code do?
#include<stdio.h> int search_num(int *arr, int num, int len) { int i; for(i = 0; i < len; i++) if(arr[i] == num) return i; return -1; } int main() { int arr[5] ={1,2,3,4,5},num=3,len = 5; int indx = search_num(arr,num,len); printf("Index of %d is %d",num,indx); return 0; }
a) Search and returns the index of all the occurrences of the number that is searched
b) Search and returns the index of the first occurrence of the number that is searched
c) Search and returns of the last occurrence of the number that is searched
d) Returns the searched element from the given array
View Answer
Explanation: The code finds the index of the first occurrence of the number that is searched.
4. What is the output of the following code?
#include<stdio.h> int search_num(int *arr, int num, int len) { int i; for(i = 0; i < len; i++) if(arr[i] == num) return i; return -1; } int main() { int arr[5] ={1,3,3,3,5},num=3,len = 5; int indx = search_num(arr,num,len); printf("Index of %d is %d",num,indx); return 0; }
a) Index of 3 is 0
b) Index of 3 is 1
c) Index of 3 is 2
d) Index of 3 is 3
View Answer
Explanation: The program prints the index of the first occurrence of 3, which is 1.
5. What is the time complexity of the following code used to search an element in an array?
#include<stdio.h> int search_num(int *arr, int num, int len) { int i; for(i = 0; i < len; i++) if(arr[i] == num) return i; return -1; } int main() { int arr[5] ={1,3,3,3,5},num=3,len = 5; int indx = search_num(arr,num,len); printf("Index of %d is %d",num,indx); return 0; }
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Explanation: The time complexity of the above code used to search an element in an array is O(n).
6. Consider the following recursive implementation of linear search:
#include<stdio.h> int recursive_search_num(int *arr, int num, int idx, int len) { if(idx == len) return -1; if(arr[idx] == num) return idx; return __________; } int main() { int arr[5] ={1,3,3,3,5},num=2,len = 5; int indx = recursive_search_num(arr,num,0,len); printf("Index of %d is %d",num,indx); return 0; }
Which of the following recursive calls should be added to complete the above code?
a) recursive_search_num(arr, num+1, idx, len);
b) recursive_search_num(arr, num, idx, len);
c) recursive_search_num(arr, num, idx+1, len);
d) recursive_search_num(arr, num+1, idx+1, len);
View Answer
Explanation: The recursive call “recursive_search_num(arr, num, idx+1, len)” should be added to complete the above code.
7. What is the output of the following code?
#include<stdio.h> int recursive_search_num(int *arr, int num, int idx, int len) { if(idx == len) return -1; if(arr[idx] == num) return idx; return recursive_search_num(arr, num, idx+1, len); } int main() { int arr[8] ={1,2,3,3,3,5,6,7},num=5,len = 8; int indx = recursive_search_num(arr,num,0,len); printf("Index of %d is %d",num,indx); return 0; }
a) Index of 5 is 5
b) Index of 5 is 6
c) Index of 5 is 7
d) Index of 5 is 8
View Answer
Explanation: The program prints the index of 5, which is 5.
8. How many times is the function recursive_search_num() called when the following code is executed?
#include<stdio.h> int recursive_search_num(int *arr, int num, int idx, int len) { if(idx == len) return -1; if(arr[idx] == num) return idx; return recursive_search_num(arr, num, idx+1, len); } int main() { int arr[8] ={1,2,3,3,3,5,6,7},num=5,len = 8; int indx = recursive_search_num(arr,num,0,len); printf("Index of %d is %d",num,indx); return 0; }
a) 5
b) 6
c) 7
d) 8
View Answer
Explanation: The function recursive_search_num() is called 6 times when the above code is executed.
9. What is the time complexity of the following recursive implementation of linear search?
#include<stdio.h> int recursive_search_num(int *arr, int num, int idx, int len) { if(idx == len) return -1; if(arr[idx] == num) return idx; return recursive_search_num(arr, num, idx+1, len); } int main() { int arr[8] ={1,2,3,3,3,5,6,7},num=5,len = 8; int indx = recursive_search_num(arr,num,0,len); printf("Index of %d is %d",num,indx); return 0; }
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Explanation: The time complexity of the above recursive implementation of linear search is O(n).
Sanfoundry Global Education & Learning Series – Data Structures & Algorithms.
To practice all areas of Data Structures & Algorithms, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice Data Structure MCQ
- Check Design and Analysis of Algorithms Books
- Check Programming Books