Data Structure Multiple Choice Questions 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
Answer: a
Clarification: 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? Answer: d 3. What does the following code do? a) Search and returns the index of all the occurrences of the number that is searched Answer: b 4. What is the output of the following code? a) Index of 3 is 0 Answer: b 5. What is the time complexity of the following code used to search an element in an array? a) O(1) Answer: b 6. Consider the following recursive implementation of linear search: Which of the following recursive calls should be added to complete the above code? Answer: c 7. What is the output of the following code? a) Index of 5 is 5 Answer: a 8. How many times is the function recursive_search_num() called when the following code is executed? a) 5 Answer: b 9. What is the time complexity of the following recursive implementation of linear search? a) O(1) Answer: b
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
Clarification: Iterative linear search, Recursive linear search and Recursive binary search can be applied to search for an element in the above given array.#include
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
Clarification: The code finds the index of the first occurrence of the number that is searched.#include
b) Index of 3 is 1
c) Index of 3 is 2
d) Index of 3 is 3
Clarification: The program prints the index of the first occurrence of 3, which is 1.#include
b) O(n)
c) O(n2)
d) O(n3)
Clarification: The time complexity of the above code used to search an element in an array is O(n).#include
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);
Clarification: The recursive call “recursive_search_num(arr, num, idx+1, len)” should be added to complete the above code.#include
b) Index of 5 is 6
c) Index of 5 is 7
d) Index of 5 is 8
View Answer
Clarification: The program prints the index of 5, which is 5.#include
b) 6
c) 7
d) 8
Clarification: The function recursive_search_num() is called 6 times when the above code is executed.#include
b) O(n)
c) O(n2)
d) O(n3)
Clarification: The time complexity of the above recursive implementation of linear search is O(n).