## 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; }

Index of -2 is 1 | |

Index of -2 is 0 | |

Index of -2 is -1 | |

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; }

Adds all the indexes of the number 0 | |

Finds the first last occurrence of the number 0 | |

Counts the number of occurrences of the number 0 | |

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?

hi = mid - 1 | |

mid = (lo + hi)/2 | |

mid = lo - 1 | |

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; }

Index of 7 is 4 | |

Index of 7 is 5 | |

Index of 7 is 6 | |

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; }

Index of 0 is 0 | |

Index of 0 is 1 | |

Index of 0 is 2 | |

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.