Q. What will be the result of binary search if the target value is not present in the array?
-
A.
Returns the index of the closest value
-
B.
Returns -1
-
C.
Returns the index of the last element
-
D.
Returns the index of the first element
Solution
If the target value is not found, binary search typically returns -1 to indicate absence.
Correct Answer:
B
— Returns -1
Learn More →
Q. Which of the following is a valid implementation of binary search in Python?
-
A.
def binary_search(arr, target): ...
-
B.
def binary_search(arr, target): for i in arr: if i == target: return i
-
C.
def binary_search(arr, target): while arr: ...
-
D.
def binary_search(arr, target): return arr.index(target)
Solution
The first option correctly defines a binary search function that takes a sorted array and a target value.
Correct Answer:
A
— def binary_search(arr, target): ...
Learn More →
Showing 1 to 2 of 2 (1 Pages)