# Data Structure Questions and Answers-Linear Search Iterative

## Data Structure Questions and Answers-Linear Search Iterative

 Question 1 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
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
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 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
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(arrint 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(arr [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]`
What is the best case for linear search?
 A O(nlogn) B O(logn) C O(n) D O(1)
Question 3 Explanation:
The element is at the head of the array, hence O(1).

 Question 4 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What is the worst case for linear search?
 A O(nlogn) B O(logn) C O(n) D O(1)
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 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
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
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.