Data Structure Questions and Answers – Search an Element in an Array using Recursion – 1

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

Answer: a
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

Answer: d
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?

advertisement
advertisement
#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

Answer: b
Explanation: The code finds the index of the first occurrence of the number that is searched.

Note: Join free Sanfoundry classes at Telegram or Youtube

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

Answer: b
Explanation: The program prints the index of the first occurrence of 3, which is 1.
advertisement

5. What is the time complexity of the following code used to search an element in an array?

advertisement
#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

Answer: b
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

Answer: c
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

Answer: a
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

Answer: b
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

Answer: b
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.