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))

  1. A.1
  2. B.2Correct answer
  3. C.3
  4. 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

All of Searching

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed