Data Structure online test on “Largest and Smallest Number in an Array using Recursion”.
1. Which of the following methods can be used to find the largest and smallest element in an array?
a) Recursion
b) Iteration
c) Both recursion and iteration
d) No method is suitable
Answer: c
Clarification: Both recursion and iteration can be used to find the largest and smallest element in an array.
2. Consider the following iterative code snippet to find the largest element:
int get_max_element(int *arr,int n) { int i, max_element = arr[0]; for(i = 1; i < n; i++) if(________) max_element = arr[i]; return max_element; }
Which of the following lines should be inserted to complete the above code?
a) arr[i] > max_element
b) arr[i] < max_element
c) arr[i] == max_element
d) arr[i] != max_element
Answer: a
Clarification: The line “arr[i] > max_element” should be inserted to complete the above code snippet.
3. Consider the following code snippet to find the smallest element in an array:
int get_min_element(int *arr, int n) { int i, min_element = arr[0]; for(i = 1; i < n; i++) if(_______) min_element = arr[i]; return min_element; }
Which of the following lines should be inserted to complete the above code?
a) arr[i] > min_element
b) arr[i] < min_element
c) arr[i] == min_element
d) arr[i] != min_element
Answer: b
Clarification: The line “arr[i] < min_element” should be inserted to complete the above code.
4. What is the output of the following code?
#includeint get_max_element(int *arr,int n) { int i, max_element = arr[0]; for(i = 1; i < n; i++) if(arr[i] > max_element) max_element = arr[i]; return max_element; } int get_min_element(int *arr, int n) { int i, min_element = arr[0]; for(i = 1; i < n; i++) if(arr[i] < min_element) min_element = arr[i]; return min_element; } int main() { int n = 7, arr[7] = {5,2,4,7,8,1,3}; int max_element = get_max_element(arr,n); int min_element = get_min_element(arr,n); printf("%d %d",max_element,min_element); return 0; }
a) 5 3 Answer: c 5. What is the output of the following code? a) 1 -1 6. What is the time complexity of the following iterative implementation used to find the largest and smallest element in an array? a) O(1) Answer: b 7. Consider the following recursive implementation to find the largest element in an array. Which of the following lines should be inserted to complete the above code? Answer: c 8. What is the output of the following code? a) -1 10 Answer: b 9. What is the time complexity of the following recursive implementation used to find the largest and the smallest element in an array? a) O(1) Answer: b 10. How many times is the function recursive_min_element() called when the following code is executed? a) 9 Answer: b 11. What is the output of the following code? a) 1 1 Answer: a
b) 3 5
c) 8 1
d) 1 8
Clarification: The program prints the values of the largest and the smallest elements in the array, which are 8 and 1 respectively.#include
b) -1 1
c) 1 Garbage value
d) Depends on the compiler
Answer: c
Clarification: Since the min_element variable is not initialised, some compilers will auto initialise as 0 which produces -1 as output whereas some compilers won’t initialise automatically. In that case, the result will be a garbage value hence the output depends on the compiler.#include
b) O(n)
c) O(n2)
d) O(n/2)
View Answer
Clarification: The time complexity of the above iterative implementation used to find the largest and the smallest element in an array is O(n).int max_of_two(int a, int b)
{
if(a > b)
return a;
return b;
}
int recursive_max_element(int *arr, int len, int idx)
{
if(idx == len - 1)
return arr[idx];
return _______;
}
a) max_of_two(arr[idx], recursive_max_element(arr, len, idx))
b) recursive_max_element(arr, len, idx)
c) max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1))
d) recursive_max_element(arr, len, idx + 1)
Clarification: The line “max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1))” should be inserted to complete the above code.#include
b) 10 -1
c) 1 10
d) 10 1
Clarification: The program prints the values of the largest and the smallest element in the array, which are 10 and -1 respectively.#include
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Clarification: The time complexity of the above recursive implementation used to find the largest and smallest element in an array is O(n).int min_of_two(int a, int b)
{
if(a < b)
return a;
return b;
}
int recursive_min_element(int *arr, int len, int idx)
{
if(idx == len - 1)
return arr[idx];
return min_of_two(arr[idx], recursive_min_element(arr, len, idx + 1));
}
int main()
{
int n = 10, idx = 0, arr[] = {5,2,6,7,8,9,3,-1,1,10};
int min_element = recursive_min_element(arr,n,idx);
printf("%d",min_element);
return 0;
}
b) 10
c) 11
d) 12
View Answer
Clarification: The function recursive_min_element() is called 10 times when the above code is executed.#include
b) 0 0
c) compile time error
d) runtime error
Clarification: The program prints the values of the largest and the smallest element in the array, which are 1 and 1.