Q. In Python, which of the following is a correct implementation of binary search?
-
A.
def binary_search(arr, x): ...
-
B.
def binary_search(arr, x): return arr.index(x)
-
C.
def binary_search(arr, x): for i in arr: if i == x: return i
-
D.
def binary_search(arr, x): while arr: ...
Solution
The correct implementation of binary search involves a function that takes an array and a target value, and uses a loop to narrow down the search.
Correct Answer:
A
— def binary_search(arr, x): ...
Learn More →
Q. What will be the output of binary search if the target element is not present in the array?
-
A.
Index of the closest element
-
B.
-1
-
C.
0
-
D.
Length of the array
Solution
If the target element is not found, binary search typically returns -1 to indicate absence.
Correct Answer:
B
— -1
Learn More →
Q. Which of the following best describes the divide-and-conquer approach used in binary search?
-
A.
Solving a problem by breaking it down into smaller subproblems
-
B.
Using a single loop to iterate through elements
-
C.
Recursively calling the same function without any condition
-
D.
Sorting the array before searching
Solution
Binary search uses the divide-and-conquer strategy by dividing the array into halves to find the target.
Correct Answer:
A
— Solving a problem by breaking it down into smaller subproblems
Learn More →
Showing 1 to 3 of 3 (1 Pages)