Chapter 8 · Class 12 · CBSE · Science Computer Science
Searching
25 practice questions · 5 marks weightage
Topics in this chapter
- Linear Search Methods
- Binary Search Tracing
- Binary Search Order and Bounds
- Insertion and Duplicate Positions
- Search Complexity and Strategy
- Recursive and Iterative Search
Cheat sheet
Cheat SheetPractice questions
- Question 1Q-86E881See the answer and solution
What is printed by the following Python code? def linear_search(a, x): for i in range(len(a)): if a[i] == x: return i return -1 print(linear_search([14, 7, 21, 9], 21))
- A.1
- B.2
- C.3
- D.-1
- Question 2Q-02D2E3See the answer and solution
Consider the following binary search function and list: def binary_search(a, x): low, high = 0, len(a) - 1 while low <= high: mid = (low + high) // 2 if a[mid] == x: return mid elif a[mid] < x: low = mid + 1 else: high = mid - 1 return -1
For a = [3, 8, 12, 17, 25, 31, 44, 50, 63] and x = 31, what are the returned index and the number of element comparisons made before finding the value?
- A.Index 5 and 2 comparisons
- B.Index 5 and 3 comparisons
- C.Index 4 and 3 comparisons
- D.Index 6 and 2 comparisons
- Question 3Q-37EDBESee the answer and solution
Why must the data be sorted before applying binary search? Also compare the worst-case time complexity of binary search with that of linear search.
- Question 4Q-0F94FBSee the answer and solution
In an iterative linear search, what value is printed by the following Python code? def count_checks(a, x): checks = 0 for value in a: checks += 1 if value == x: break return checks print(count_checks([6, 1, 4, 9, 2], 9))
- A.3
- B.4
- C.5
- D.9
- Question 5Q-AE88ACSee the answer and solution
In binary search on an ascending sorted list, what should be done when a[mid] is less than the target value x?
- A.Set high = mid - 1
- B.Set low = mid + 1
- C.Set low = mid - 1
- D.Stop the search because x is absent
20 more questions on Searching
Sign up free to practise the full chapter. Questions you get wrong come back until you've mastered them.
Start practising freeFree to start · No credit card needed