• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
Switch to Dark Mode

Top MCQs on Searching Algorithm with Answers

Last Updated : Mar 19, 2024
Discuss
Comments

Question 1

Linear search is also called------

  • A

    Random Search

  • B

    Sequential search

  • C

    Perfect search

  • D

    None

Question 2

Given a sorted array of integers, what can be the minimum worst-case time complexity to find ceiling of a number x in given array? The ceiling of an element x is the smallest element present in array which is greater than or equal to x. Ceiling is not present if x is greater than the maximum element present in array. For example, if the given array is {12, 67, 90, 100, 300, 399} and x = 95, then the output should be 100.

  • A

    O(loglogn)

  • B

    O(n)

  • C

    O(log(n))

  • D

    O(log(n) * log(n))

Question 3

The increasing order of performance  of the searching algorithms are:

  • A

    linear search  <  jump search  <  binary search

  • B

    linear search  >  jump search  <  binary search

  • C

    linear search  <  jump search  >  binary search

  • D

    linear search  >  jump search  >  binary search

Question 4

The average case occurs in the Linear Search Algorithm when:

  • A

    The item to be searched is in some where middle of the Array

  • B

    The item to be searched is not in the array

  • C

    The item to be searched is in the last of the array

  • D

    The item to be searched is either in the last or not in the array

Question 5

Consider the C function given below. Assume that the array listA contains n (> 0) elements, sorted in ascending order. 

C++
#include <iostream>
using namespace std;

int ProcessArray(int *listA, int x, int n)
{
    int i, j, k;
    i = 0;
    j = n-1;
    do
    {
        k = (i+j)/2;
        if (x <= listA[k])
            j = k-1;
        if (listA[k] <= x)
            i = k+1;
    }
    while (i <= j);
    if (listA[k] == x)
        return(k);
    else
        return -1;
}
C
#include <stdio.h>

int ProcessArray(int *listA, int x, int n)
{
    int i, j, k;
    i = 0;
    j = n-1;
    do
    {
        k = (i+j)/2;
        if (x <= listA[k])
            j = k-1;
        if (listA[k] <= x)
            i = k+1;
    }
    while (i <= j);
    if (listA[k] == x)
        return(k);
    else
        return -1;
}
Java
public class Main {
    public static int ProcessArray(int[] listA, int x, int n) {
        int i = 0, j = n - 1, k;
        do {
            k = (i + j) / 2;
            if (x <= listA[k])
                j = k - 1;
            if (listA[k] <= x)
                i = k + 1;
        } while (i <= j);
        if (listA[k] == x)
            return k;
        else
            return -1;
    }
}
Python
def ProcessArray(listA, x, n):
    i = 0
    j = n - 1
    while i <= j:
        k = (i + j) // 2
        if x <= listA[k]:
            j = k - 1
        if listA[k] <= x:
            i = k + 1
    if listA[k] == x:
        return k
    else:
        return -1
JavaScript
function ProcessArray(listA, x, n) {
    let i = 0;
    let j = n - 1;
    let k;
    do {
        k = Math.floor((i + j) / 2);
        if (x <= listA[k])
            j = k - 1;
        if (listA[k] <= x)
            i = k + 1;
    } while (i <= j);
    if (listA[k] === x)
        return k;
    else
        return -1;
}

Which one of the following statements about the function ProcessArray is CORRECT?

  • A

    It will run into an infinite loop when x is not in listA.

  • B

    It is an implementation of binary search.

  • C

    It will always find the maximum element in listA.

  • D

    It will return −1 even when x is present in listA.

Question 6

The average number of key comparisons done in a successful sequential search in a list of length n is

  • A

    log n

  • B

    (n-1)/2

  • C

    n/2

  • D

    (n+1)/2

Question 7

Consider the following program that attempts to locate an element x in a sorted array a[ ] using binary search. Assume N>1. The program is erroneous. Under what conditions does the program fail?

C++
#include <iostream>
#include <vector>
using namespace std;

int find(vector<int>a, int n) {
    int i = 1, j = n;
    int x;
    do {
        int k = (i + j) / 2;
        if (a[k] < x) i = k + 1;
        else j = k;
    } while (a[k] != x && i < j);
    
    if (a[k] == x)
        cout << "x is in the array" << endl;
    else
        cout << "x is not in the array" << endl;
    return 0;
}
C
#include <stdio.h>
#include <stdbool.h>

int find(int a[], int n, int x) {
    int i = 1, j = n;
    int k;
    do {
        k = (i + j) / 2;
        if (a[k] < x) i = k + 1;
        else j = k;
    } while (a[k] != x && i < j);
    
    if (a[k] == x)
        printf("x is in the array\n");
    else
        printf("x is not in the array\n");
    return 0;
}
Java
import java.util.List;

public class Main {
    public static void find(int arr[], int n, int x) {
        int i = 0, j = n;
        int k;
        do {
            k = (i + j) / 2;
            if (arr[k] < x) i = k + 1;
            else j = k;
        } while (i < j && arr[k] != x);

        if (arr[k] == x)
            System.out.println("x is in the array");
        else
            System.out.println("x is not in the array");
    }
}
Python
def find(a, n, x):
    i = 0
    j = n
    while i < j:
        k = (i + j) // 2
        if a[k] < x:
            i = k + 1
        else:
            j = k
    
    if i < len(a) and a[i] == x:
        print("x is in the array")
    else:
        print("x is not in the array")
JavaScript
function find(a, n, x) {
    let i = 0, j = n;
    let k;
    do {
        k = Math.floor((i + j) / 2);
        if (a[k] < x) i = k + 1;
        else j = k;
    } while (a[k] !== x && i < j);
    
    if (a[k] === x)
        console.log("x is in the array");
    else
        console.log("x is not in the array");
}


  • A

    x is the last element of the array a[]

  • B

    x is greater than all elements of the array a[]

  • C

    Both of the Above

  • D

    x is less than the last element of the array a[]

Question 8

The necessary condition for using binary search in an array is :-

  • A

    The array should not be too long

  • B

    The array should of more size

  • C

    The array should be sorted 

  • D

    None of these

Question 9

what is the name of the below searching Algorithm?

C++
int function(vector<int> arr,int x){
	int n = arr.size();
	
	if(n == 0)
		return -1;

	// Find range for binary search by repeatedly doubling i
	int i = 1;
	while(i < n and arr[i] < x)
		i *= 2;

	// Perform binary search on the range [i/2, min(i, n-1)]
	int left = i /2;
	int right = min(i, n-1);

	while(left <= right){
		int mid = (left + right)/2;
		
		if(arr[mid] == x) return mid;
		else if(arr[mid] < x) left = mid + 1;
		else right = mid - 1;
	}

	return -1;
}
C
#include <stdio.h>
#include <stddef.h>

int binary_search(int arr[], int n, int x) {
    if(n == 0)
        return -1;

    // Find range for binary search by repeatedly doubling i
    int i = 1;
    while(i < n && arr[i] < x)
        i *= 2;

    // Perform binary search on the range [i/2, min(i, n-1)]
    int left = i / 2;
    int right = (i < n) ? i : n - 1;

    while(left <= right) {
        int mid = (left + right) / 2;
        
        if(arr[mid] == x) return mid;
        else if(arr[mid] < x) left = mid + 1;
        else right = mid - 1;
    }

    return -1;
}
Java
import java.util.*;

public class Main {
    public static int function(List<Integer> arr, int x) {
        int n = arr.size();
        
        if (n == 0)
            return -1;

        // Find range for binary search by repeatedly doubling i
        int i = 1;
        while (i < n && arr.get(i) < x)
            i *= 2;

        // Perform binary search on the range [i/2, min(i, n-1)]
        int left = i / 2;
        int right = Math.min(i, n - 1);

        while (left <= right) {
            int mid = (left + right) / 2;

            if (arr.get(mid) == x) return mid;
            else if (arr.get(mid) < x) left = mid + 1;
            else right = mid - 1;
        }

        return -1;
    }
}
Python
def function(arr, x):
    n = len(arr)
    
    if n == 0:
        return -1

    # Find range for binary search by repeatedly doubling i
    i = 1
    while i < n and arr[i] < x:
        i *= 2

    # Perform binary search on the range [i/2, min(i, n-1)]
    left = i // 2
    right = min(i, n - 1)

    while left <= right:
        mid = (left + right) // 2

        if arr[mid] == x:
            return mid
        elif arr[mid] < x:
            left = mid + 1
        else:
            right = mid - 1

    return -1
JavaScript
function functionSearch(arr, x) {
    let n = arr.length;
    
    if (n === 0)
        return -1;

    // Find range for binary search by repeatedly doubling i
    let i = 1;
    while (i < n && arr[i] < x)
        i *= 2;

    // Perform binary search on the range [i/2, Math.min(i, n-1)]
    let left = Math.floor(i / 2);
    let right = Math.min(i, n - 1);

    while (left <= right) {
        let mid = Math.floor((left + right) / 2);

        if (arr[mid] === x) return mid;
        else if (arr[mid] < x) left = mid + 1;
        else right = mid - 1;
    }

    return -1;
}
  • A

    Linear Search

  • B

    Sequential Search

  • C

    Jump  Search

  • D

    Exponential Search

Question 10

What is the time complexity for performing Jump search?

  • A

    O(LogN)

  • B

    O(N)

  • C

    O(N^2)

  • D

    O(√N)

Master DSA and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

Step into the Three 90 Challenge – 90 days to push limits, break barriers, and become the best version of yourself! Don't miss your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!

There are 15 questions to complete.

Take a part in the ongoing discussion