Q. In a binary search algorithm, if the middle element is greater than the target, what should be done next?
-
A.
Search the left half
-
B.
Search the right half
-
C.
Return the middle element
-
D.
Increase the middle index
Solution
If the middle element is greater than the target, the search continues in the left half of the array.
Correct Answer:
A
— Search the left half
Learn More →
Q. What happens if the target value is not found in the array during binary search?
-
A.
Returns the index of the closest value
-
B.
Returns -1
-
C.
Returns the last index
-
D.
Throws an exception
Solution
If the target value is not found, binary search typically returns -1 to indicate absence.
Correct Answer:
B
— Returns -1
Learn More →
Q. What is the result of binary search if the array is empty?
-
A.
Returns 0
-
B.
Returns -1
-
C.
Returns null
-
D.
Throws an error
Solution
If the array is empty, binary search will return -1, indicating that the target is not found.
Correct Answer:
B
— Returns -1
Learn More →
Q. Which of the following best describes the space complexity of binary search?
-
A.
O(n)
-
B.
O(log n)
-
C.
O(1)
-
D.
O(n log n)
Solution
Binary search has a space complexity of O(1) when implemented iteratively, as it uses a constant amount of space.
Correct Answer:
C
— O(1)
Learn More →
Q. Which of the following C++ functions can be used to implement binary search on a sorted array?
-
A.
std::find
-
B.
std::search
-
C.
std::binary_search
-
D.
std::linear_search
Solution
The std::binary_search function from the C++ Standard Library can be used to perform binary search on a sorted array.
Correct Answer:
C
— std::binary_search
Learn More →
Showing 1 to 5 of 5 (1 Pages)