Searching · Class 12 · CBSE · Science Computer Science
What is printed by the following Python code? def linearsearch(a, x): for i in range(len(a)): if a[i] == x: return i return -1 print(linearsearch([14…
Practice question from Searching (Class 12 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.
easy · MCQ · Q-86E881
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.2Correct answer
- C.3
- D.-1
Solution
The value 21 is found at index 2 because Python list indexing starts from 0. Therefore, the function returns and prints 2.
More from Searching
- Consider the following binary search function and list: def binarysearch(a, x): low, high = 0, len(a) - 1 while low <= high: mid = (low + h…
- Why must the data be sorted before applying binary search? Also compare the worst-case time complexity of binary search with that of linear…
- In an iterative linear search, what value is printed by the following Python code? def countchecks(a, x): checks = 0 for value in a: checks…
- In binary search on an ascending sorted list, what should be done when a[mid] is less than the target value x?
Practise more Computer Science
Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.
Start practising freeFree to start · No credit card needed