Q. How does binary search determine which half of the array to search next?
A.
By comparing the middle element with the target
B.
By checking the length of the array
C.
By using a random index
D.
By iterating through the array
Show solution
Solution
Binary search compares the middle element of the array with the target value to decide whether to search the left or right half.
Correct Answer:
A
— By comparing the middle element with the target
Learn More →
Q. In which scenario can binary search be applied?
A.
Unsorted array
B.
Sorted array
C.
Linked list
D.
Stack
Show solution
Solution
Binary search can only be applied to a sorted array, as it relies on the order of elements to eliminate half of the search space.
Correct Answer:
B
— Sorted array
Learn More →
Q. What is the space complexity of binary search when implemented iteratively?
A.
O(n)
B.
O(log n)
C.
O(1)
D.
O(n log n)
Show solution
Solution
The space complexity of iterative binary search is O(1) since it uses a constant amount of space regardless of the input size.
Correct Answer:
C
— O(1)
Learn More →
Q. What is the worst-case scenario for the number of comparisons in binary search?
A.
n
B.
log n
C.
n log n
D.
1
Show solution
Solution
In the worst case, binary search will make log n comparisons, where n is the number of elements in the array.
Correct Answer:
B
— log n
Learn More →
Q. What will be the return value of binary search if the element is not found?
Show solution
Solution
Typically, binary search returns -1 to indicate that the element was not found in the array.
Correct Answer:
A
— -1
Learn More →
Q. Which of the following C++ functions correctly implements binary search?
A.
int binarySearch(int arr[], int l, int r, int x)
B.
void binarySearch(int arr[], int x)
C.
int binarySearch(int arr[], int x)
D.
bool binarySearch(int arr[], int l, int r)
Show solution
Solution
The function 'int binarySearch(int arr[], int l, int r, int x)' is a correct implementation signature for binary search, taking the array, left index, right index, and the target value.
Correct Answer:
A
— int binarySearch(int arr[], int l, int r, int x)
Learn More →
Q. Which of the following is a requirement for binary search to work?
A.
The array must be sorted
B.
The array must be of even length
C.
The array must contain unique elements
D.
The array must be in ascending order
Show solution
Solution
The primary requirement for binary search is that the array must be sorted, regardless of whether it contains unique elements or its length.
Correct Answer:
A
— The array must be sorted
Learn More →
Q. Which of the following statements about binary search is false?
A.
It can be implemented recursively
B.
It requires a sorted array
C.
It can be used on linked lists
D.
It is faster than linear search
Show solution
Solution
Binary search cannot be effectively used on linked lists due to their non-contiguous memory allocation, making it inefficient compared to arrays.
Correct Answer:
C
— It can be used on linked lists
Learn More →
Showing 1 to 8 of 8 (1 Pages)