Sail E0 Webinar

MCQs

Total Questions : 10
Question 1. What are the applications of binary search?
  1.    To find the lower/upper bound in an ordered sequence
  2.    Union of intervals
  3.    Debugging
  4.    All of the mentioned
 Discuss Question
Answer: Option D. -> All of the mentioned


All of the mentioned can be realized by binary search.


Question 2. What is the average case time complexity of binary search using recursion?
  1.    O(nlogn)
  2.    O(logn)
  3.    O(n)
  4.    O(n2)
 Discuss Question
Answer: Option B. -> O(logn)


T(n) = T(n/2) + 1, Using the divide and conquer master theorem.


Question 3. What is the length of the step in jump search?
  1.    n
  2.    n/2
  3.    sqrt(n)
  4.    1
 Discuss Question
Answer: Option C. -> sqrt(n)


If the step size is 1, it becomes a linear search, if it is n, we reach the end of the list in just on step, if it is n/2, it becomes similar to binary search, therefore the most efficient step size is found to be sqrt(n).


Question 4. Select the code snippet for Jump Search.
  1.    public int jumpSearch(int arr[], int key){
  2.    public int jumpSearch(int arr[], int key){
  3.    public int jumpSearch(int arr[], int key){
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> public int jumpSearch(int arr[], int key){


After finding the correct block of k elements, a sequential search is performed in this block.


Question 5. What is the best case and worst case complexity of ordered linear search?
  1.    O(nlogn), O(logn)
  2.    O(logn), O(nlogn)
  3.    O(n), O(1)
  4.    O(1), O(n)
 Discuss Question
Answer: Option D. -> O(1), O(n)


Although ordered linear search is better than unordered when the element is not present in the array, the best and worst cases still remain the same, with the key element being found at first position or at last position.


Question 6. Choose the code snippet which uses recursion for linear search.
  1.    public void linSearch(int[] arr, int first, int last, int key){
  2.    public void linSearch(int[] arr, int first, int last, int key)      {
  3.    public void linSearch(int[] arr, int first, int last, int key){
  4.    public void linSearch(int[] arr, int first, int last, int key){
 Discuss Question
Answer: Option A. -> public void linSearch(int[] arr, int first, int last, int key){


Every time check the key with the array value at first index, if it is not equal then call the function again with an incremented first index.


Question 7. Given, arr = {1,3,5,6,7,9,14,15,17,19} key = 17 and delta = {5,3,1,0}
How many key comparisons are made?(exclude the comparison used to decide the left or right sub array)
  1.    4
  2.    3
  3.    5
  4.    6
 Discuss Question
Answer: Option B. -> 3


Tracing with the above code, comparison #1: i=4, comparison #2: i=7, comparison #3: i=8


Question 8. What does the following piece of code do?for (int i = 0; i < arr.length-1; i++){    for (int j = i+1; j < arr.length; j++)    {        if( (arr[i].equals(arr[j])) && (i != j) )        {            System.out.println(arr[i]);        }    }}
  1.    Print the duplicate elements in the array
  2.    Print the element with maximum frequency
  3.    Print the unique elements in the array
  4.    None of the mentioned
 Discuss Question
Answer: Option A. -> Print the duplicate elements in the array


The print statement is executed only when the items are equal and their indices are not.


Question 9. How can Jump Search be improved?
  1.    Start searching from the end
  2.    Begin from the kth item, where k is the step size
  3.    Cannot be improved
  4.    Step size should be other than sqrt(n)
 Discuss Question
Answer: Option B. -> Begin from the kth item, where k is the step size


This gives a very slight improvement as you are skipping the first k elements.


Question 10. Choose among the following code for an iterative binary search.
  1.    public static int iterative(int arr[], int key){
  2.    public static int iterative(int arr[], int key){
  3.    public static int iterative(int arr[], int key){
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> public static int iterative(int arr[], int key){


Find the 'mid', check if it equals the key, if not, continue the iterations until low

Latest Videos

Latest Test Papers