This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: The line “arr[i] < min_element” should be inserted to complete the above code.
4. What is the output of the following code?
#include<stdio.h> int 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
b) 3 5
c) 8 1
d) 1 8
View Answer
Explanation: The program prints the values of the largest and the smallest elements in the array, which are 8 and 1 respectively.
5. What is the output of the following code?
#include<stdio.h> int 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; 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] = {1,1,1,0,-1,-1,-1}; 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) 1 -1
b) -1 1
c) 1 Garbage value
d) Depends on the compiler
View Answer
Explanation: 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.
6. What is the time complexity of the following iterative implementation used to find the largest and smallest element in an array?
#include<stdio.h> int 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; 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] = {1,1,1,0,-1,-1,-1}; 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) O(1)
b) O(n)
c) O(n2)
d) O(n/2)
View Answer
Explanation: The time complexity of the above iterative implementation used to find the largest and the smallest element in an array is O(n).
7. Consider the following recursive implementation to find the largest element in an array.
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 _______; }
Which of the following lines should be inserted to complete the above code?
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)
View Answer
Explanation: The line “max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1))” should be inserted to complete the above code.
8. What is the output of the following code?
#include<stdio.h> int max_of_two(int a, int b) { if(a > b) return a; return b; } int min_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 max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1)); } 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 max_element = recursive_max_element(arr,n,idx); int min_element = recursive_min_element(arr,n,idx); printf("%d %d",max_element,min_element); return 0; }
a) -1 10
b) 10 -1
c) 1 10
d) 10 1
View Answer
Explanation: The program prints the values of the largest and the smallest element in the array, which are 10 and -1 respectively.
9. What is the time complexity of the following recursive implementation used to find the largest and the smallest element in an array?
#include<stdio.h> int max_of_two(int a, int b) { if(a > b) return a; return b; } int min_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 max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1)); } 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 max_element = recursive_max_element(arr,n,idx); int min_element = recursive_min_element(arr,n,idx); printf("%d %d",max_element,min_element); 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 used to find the largest and smallest element in an array is O(n).
10. How many times is the function recursive_min_element() called when the following code is executed?
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; }
a) 9
b) 10
c) 11
d) 12
View Answer
Explanation: The function recursive_min_element() is called 10 times when the above code is executed.
11. What is the output of the following code?
#include<stdio.h> int max_of_two(int a, int b) { if(a > b) return a; return b; } int min_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 max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1)); } 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 = 5, idx = 0, arr[] = {1,1,1,1,1}; int max_element = recursive_max_element(arr,n,idx); int min_element = recursive_min_element(arr,n,idx); printf("%d %d",max_element,min_element); return 0; }
a) 1 1
b) 0 0
c) compile time error
d) runtime error
View Answer
Explanation: The program prints the values of the largest and the smallest element in the array, which are 1 and 1.
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
- Practice Programming MCQs
- Check Computer Science Books
- Practice Data Structure MCQ
- Apply for Computer Science Internship