Q. If the array is sorted in descending order, can binary search still be applied?
-
A.
Yes, with modifications
-
B.
No, it cannot be applied
-
C.
Yes, without any changes
-
D.
Only for specific cases
Solution
Binary search can be applied to a descending sorted array with modifications to the comparison logic.
Correct Answer:
A
— Yes, with modifications
Learn More →
Q. If you have a sorted array of 1000 elements, how many iterations will binary search take at most?
Solution
Binary search will take at most log2(1000) which is approximately 10 iterations.
Correct Answer:
A
— 10
Learn More →
Q. In a binary search, if the target value is not present in the array, what will be the return value?
-
A.
-1
-
B.
0
-
C.
null
-
D.
the index of the closest value
Solution
Typically, binary search returns -1 to indicate that the target value is not found in the array.
Correct Answer:
A
— -1
Learn More →
Q. In binary search, how do you calculate the middle index?
-
A.
(low + high) / 2
-
B.
(low + high) / 2 + 1
-
C.
(low + high) / 2 - 1
-
D.
low + (high - low) / 2
Solution
The middle index is calculated using low + (high - low) / 2 to avoid overflow.
Correct Answer:
D
— low + (high - low) / 2
Learn More →
Q. What happens if the middle element in binary search is equal to the target?
-
A.
Search continues in the left half
-
B.
Search continues in the right half
-
C.
Target is found
-
D.
Search fails
Solution
If the middle element is equal to the target, the search is successful and the target is found.
Correct Answer:
C
— Target is found
Learn More →
Q. What is the worst-case scenario for the number of comparisons made by binary search on an array of size n?
-
A.
n
-
B.
log n
-
C.
n log n
-
D.
1
Solution
In the worst case, binary search makes log n comparisons, where n is the number of elements in the array.
Correct Answer:
B
— log n
Learn More →
Showing 1 to 6 of 6 (1 Pages)