Data Structure Questions and Answers-Linear Search Iterative

 

 Buy/Download all MCQ Ebook   >>>Click Here<<<

Data Structure Questions and Answers-Linear Search Iterative

Click on any option to know the CORRECT ANSWERS

Question 1
Where is linear searching used?
A
When the list has only a few elements
B
When performing a single search in an unordered list
C
Used all the time
D
When the list has only a few elements and When performing a single search in an unordered list
Home science Questions answers

Question 1 Explanation: 
It is practical to implement linear search in the situations mentioned in When the list has only a few elements and When performing a single search in an unordered list, but for larger elements the complexity becomes larger and it makes sense to sort the list and employ binary search or hashing.

Question 2
Select the code snippet which performs unordered linear search iteratively?
A

int unorderedLinearSearch(int arr[], int size, int data) { int index; for(int i = 0; i < size; i++) { if(arr
int unorderedLinearSearch(int arr[], int size, int data) { int index; for(int i = 0; i < size; i++) { if(arr
C

int unorderedLinearSearch(int arr[], int size, int data) { int index; for(int i = 0; i <= size; i++) { if(arrPuzzles Questions answers 

Question 2 Explanation: 
Unordered term refers to the given array, that is, the elements need not be ordered. To search for an element in such an array, we need to loop through the elements until the desired element is found.

Question 3
What is the best case for linear search?
A
O(nlogn)
B
O(logn)
C
O(n)
D
O(1)
Puzzles Questions answers

Question 3 Explanation: 
The element is at the head of the array, hence O(1).

Question 4
What is the worst case for linear search?
A
O(nlogn)
B
O(logn)
C
O(n)
D
O(1)
English grammar Questions answers

Question 4 Explanation: 
Worst case is when the desired element is at the tail of the array or not present at all, in this case you have to traverse till the end of the array, hence the complexity is O(n).

Question 5
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) {
B
public int linearSearch(int arr[], int key, int size) { int index = -1; 	 int i = 0; while(size > 0) { 
C

public int linearSearch(int arr[], int key, int size) { int index = -1; 	 int i = 0; while(size > 0) {
D
None of the mentioned

History Questions answers

Question 5 Explanation: 
The term ordered refers to the items in the array being sorted(here we assume ascending order). So traverse through the array until the element, if at any time the value at i exceeds key value, it means the element is not present in the array. This provides a slightly better efficiency than unordered linear search.

There are 5 questions to complete.

 

 Buy/Download all MCQ Ebook >>>CLICK HERE<<<