adplus-dvertising

Welcome to the Sorting and Searching MCQs Page

Dive deep into the fascinating world of Sorting and Searching with our comprehensive set of Multiple-Choice Questions (MCQs). This page is dedicated to exploring the fundamental concepts and intricacies of Sorting and Searching, a crucial aspect of Data Structures and Algorithms. In this section, you will encounter a diverse range of MCQs that cover various aspects of Sorting and Searching, from the basic principles to advanced topics. Each question is thoughtfully crafted to challenge your knowledge and deepen your understanding of this critical subcategory within Data Structures and Algorithms.

frame-decoration

Check out the MCQs below to embark on an enriching journey through Sorting and Searching. Test your knowledge, expand your horizons, and solidify your grasp on this vital area of Data Structures and Algorithms.

Note: Each MCQ comes with multiple answer choices. Select the most appropriate option and test your understanding of Sorting and Searching. You can click on an option to test your knowledge before viewing the solution for a MCQ. Happy learning!

Sorting and Searching MCQs | Page 15 of 24

Explore more Topics under Data Structures and Algorithms

Q141.
What is the best case for linear search?
Discuss
Answer: (d).O(1)
Q142.
What is the worst case for linear search?
Discuss
Answer: (c).O(n)
Q143.
Select the code snippet which performs ordered linear search iteratively?
a)

public int linearSearch(int arr[],int key,int size) 
{
       int index = -1;
    int i = 0;
       while(size > 0) 
       {
             if(data[i] == key) 
             {
    index = i;
             }
             if(data[i] > key)) 
             {
   index = i;
                 break;
             }
             i++;
       }
       return index;
}

b)

public int linearSearch(int arr[],int key,int size) 
{
       int index = -1;
    int i = 0;
       while(size > 0) 
       {
             if(data[i] == key) 
             {
    index = i;
             }
             if(data[i] > key)) 
             {
                  break;
             }
             i++;
       }
       return index;
}

c)

public int linearSearch(int arr[],int key,int size) 
{
       int index = -1;
    int i = 0;
       while(size > 0) 
       {
             if(data[i] == key) 
             {
  break;
             }
             if(data[i] > key)) 
             {
                 index = i;
             }
             i++;
        }
        return index;
}

d) None of the mentioned

a.

a

b.

b

c.

c

d.

d

Discuss
Answer: (b).b
Q144.
What is the best case and worst case complexity of ordered linear search?
Discuss
Answer: (d).O(1), O(n)
Q145.
Choose the code snippet which uses recursion for linear search.
a)

public void linSearch(int[] arr, int first, int last, int key)
{
 if(first == last)
        {
  System.out.print("-1");
 }
 else
        {
  if(arr[first] == key)
                {
   System.out.print(first);
  }
  else
                {
   linSearch(arr, first+1, last, key);
  }
 }
}

b)

      public void linSearch(int[] arr, int first, int last, int key)
      {
  if(first == last)
                {
   System.out.print("-1");
  }
  else
                {
   if(arr[first] == key)
                        {
    System.out.print(first);
   }
   else
                        {
    linSearch(arr, first+1, last-1, key);
   }
  }
      }

c)

public void linSearch(int[] arr, int first, int last, int key)
{
 if(first == last)
        {
  System.out.print("-1");
 }
 else
        {
  if(arr[first] == key)
                {
   System.out.print(last);
  }
  else
                {
   linSearch(arr, first+1, last, key);
  }
 }
}

d)

public void linSearch(int[] arr, int first, int last, int key)
{
 if(first == last)
        {
  System.out.print("-1");
 }
 else
        {
  if(arr[first] == key)
                {
   System.out.print(first);
  }
  else
                {
   linSearch(arr, first+1, last+1, key);
  }
 }
}

a.

a

b.

b

c.

c

d.

d

Discuss
Answer: (a).a
Q146.
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]);
        }
    }
}
Discuss
Answer: (a).Print the duplicate elements in the array
Q147.
Select the code snippet which prints the element with maximum frequency.
a)

public int findPopular(int[] a) 
{
 if (a == null || a.length == 0)
  return 0;
 Arrays.sort(a);
 int previous = a[0];
 int popular = a[0];
 int count = 1;
 int maxCount = 1;
 for (int i = 1; i < a.length; i++)
        {
  if (a[i] == previous)
  count++;
  else 
                {
   if (count > maxCount) 
                        {
    popular = a[i-1];
    maxCount = count;
   }
  previous = a[i];
  count = 1;
  }
 }
 return count > maxCount ? a[a.length-1] : popular;
}

b)

public int findPopular(int[] a) 
{
 if (a == null || a.length == 0)
  return 0;
 Arrays.sort(a);
 int previous = a[0];
 int popular = a[0];
 int count = 1;
 int maxCount = 1;
 for (int i = 1; i < a.length; i++) 
        {
  if (a[i] == previous)
   count++;
  else 
                {
   if (count > maxCount) 
                        {
    popular = a[i];
    maxCount = count;
   }
   previous = a[i];
   count = 1;
  }
 }
 return count > maxCount ? a[a.length-1] : popular;
}

c)

public int findPopular(int[] a) 
{
 if (a == null || a.length == 0)
  return 0;
 Arrays.sort(a);
 int previous = a[0];
 int popular = a[0];
 int count = 1;
 int maxCount = 1;
 for (int i = 1; i < a.length; i++) 
        {
  if (a[i+1] == previous)
   count++;
  else 
                {
   if (count > maxCount) 
                        {
    popular = a[i-1];
    maxCount = count;
   }
   previous = a[i];
   count = 1;
  }
 }
 return count > maxCount ? a[a.length-1] : popular;
}

d) None of the mentioned

a.

a

b.

b

c.

c

d.

d

Discuss
Answer: (a).a
Discuss
Answer: (b).Greater time complexities compared to other searching algorithms
Q149.
What is the advantage of recursive approach than an iterative approach?
Discuss
Answer: (b).Less code and easy to implement
Q150.
Choose the appropriate code that does binary search using recursion.
a)

public static int recursive(int arr[], int low, int high, int key)
{
 int mid = low + (high - low)/2;
 if(arr[mid] == key)
 {
  return mid;
 }
 else if(arr[mid] < key)
 {
  return recursive(arr,mid+1,high,key);
 }
 else
 {
  return recursive(arr,low,mid-1,key);
 }
}

b)

public static int recursive(int arr[], int low, int high, int key)
{
 int mid = low + (high + low)/2;
 if(arr[mid] == key)
 {
  return mid;
 }
 else if(arr[mid] < key)
 {
  return recursive(arr,mid-1,high,key);
 }
 else
 {
  return recursive(arr,low,mid+1,key);
 }
}

c)

public static int recursive(int arr[], int low, int high, int key)
{
 int mid = low + (high - low)/2;
 if(arr[mid] == key)
 {
  return mid;
 }
 else if(arr[mid] < key)
 {
  return recursive(arr,mid,high,key);
 }
 else
 {
  return recursive(arr,low,mid-1,key);
 }
}

d) None of the mentioned

a.

a

b.

b

c.

c

d.

d

Discuss
Answer: (a).a

Suggested Topics

Are you eager to expand your knowledge beyond Data Structures and Algorithms? We've curated a selection of related categories that you might find intriguing.

Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!