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

 

 Help authour, Buy PDF Ebook   >>>Click Here<<<

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

Click on any option to know the CORRECT ANSWERS

Question 1
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] ={-11, 2, -3, 0, 3, 5, -6, 7}, num = -2, len = 8; int indx = recursive....search....num(arr, num, 0, len); printf("Index of %d is %d", num, indx); return 0; }
A
Index of -2 is 1
B
Index of -2 is 0
C
Index of -2 is -1
D
None of the mentioned
Management Questions answers

Question 1 Explanation: 
The program prints the index of the first occurrence of -2. Since, -2 doesn't exist in the array, the program prints -1.

Question 2
What does the following code do?

#include<stdio.h> int cnt = 0; int recursive....search....num(int *arr,  int num,  int idx,  int len) { int cnt = 0; if(idx == len) return cnt; if(arr[idx] == num) cnt++; return cnt + recursive....search....num(arr,  num,  idx+1,  len); } int main() { int arr[8] ={0, 0, 0, 0, 3, 5, -6, 7}, num = 0, len = 8; int ans = recursive....search....num(arr, num, 0, len); printf("%d", ans); return 0; }
A
Adds all the indexes of the number 0
B
Finds the first last occurrence of the number 0
C
Counts the number of occurrences of the number 0
D
None of the mentioned
English grammar Questions answers

Question 2 Explanation: 
The above code counts the number of occurrences of the number 0.

Question 3
Consider the following recursive implementation of the binary search:

#include<stdio.h> int recursive....binary....search(int *arr,  int num,  int lo,  int hi) { if(lo > hi) return -1; int mid = (lo + hi)/2; if(arr[mid] == num) return mid; else if(arr[mid] < num) .....; else hi = mid - 1; return recursive....binary....search(arr,  num,  lo,  hi); } int main() { int arr[8] ={0, 0, 0, 0, 3, 5, 6, 7}, num = 7, len = 8; int indx = recursive....binary....search(arr, num, 0, len-1); printf("Index of %d is %d", num, indx); return 0; }

Which of the following lines should be added to complete the above code?

A
hi = mid - 1
B
mid = (lo + hi)/2
C
mid = lo - 1
D
lo = mid + 1
ICT Questions answers

Question 3 Explanation: 
The line "lo = mid + 1" should be added to complete the above code.

Question 4
What is the output of the following code?

#include<stdio.h> int recursive....binary....search(int *arr,  int num,  int lo,  int hi) { if(lo > hi) return -1; int mid = (lo + hi)/2; if(arr[mid] == num) return mid; else if(arr[mid] < num) lo = mid + 1; else hi = mid - 1; return recursive....binary....search(arr,  num,  lo,  hi); } int main() { int arr[8] = {1, 2, 3, 4, 5, 6, 7, 8}, num = 7, len = 8; int indx = recursive....binary....search(arr, num, 0, len-1); printf("Index of %d is %d", num, indx); return 0; }
A
Index of 7 is 4
B
Index of 7 is 5
C
Index of 7 is 6
D
Index of 7 is 7
English grammar Questions answers

Question 4 Explanation: 
The program prints the index of number 7, which is 6.

Question 5
What is the output of the following code?

#include<stdio.h> int recursive....binary....search(int *arr,  int num,  int lo,  int hi) { if(lo > hi) return -1; int mid = (lo + hi)/2; if(arr[mid] == num) return mid; else if(arr[mid] < num) lo = mid + 1; else hi = mid - 1; return recursive....binary....search(arr,  num,  lo,  hi); } int main() { int arr[8] = {0, 0, 0, 0, 3, 5, 6, 7}, num = 0, len = 8; int indx = recursive....binary....search(arr, num, 0, len-1); printf("Index of %d is %d", num, indx); return 0; }
A
Index of 0 is 0
B
Index of 0 is 1
C
Index of 0 is 2
D
Index of 0 is 3
English grammar Questions answers

Question 5 Explanation: 
In this case, when the function recursive....binary....search() is called for the first time we have: lo = 0 and hi = 7. So, the value of mid is:

mid = (lo + hi)/2 = (0 + 7)/2 = 3. Since, arr[mid] = arr[3] = 0, the function returns the value of mid, which is 3.

There are 5 questions to complete.