logo

AQA A-Level Computer Science practice

Trace this Python linear search for target = 5 and data = [8, 1, 5, 3]. State the values checked in order, the number of comparisons, and the result of the search. for index in range(len(data)): if data[index] == target: print(index) break

Try the question first, then check the answer and practise the same topic again.

At a glance

Question

Type

practice

Style

Topic

Searching algorithms

Exam-style question

Try this first

Trace this Python linear search for target = 5 and data = [8, 1, 5, 3]. State the values checked in order, the number of comparisons, and the result of the search. for index in range(len(data)): if data[index] == target: print(index) break.

Answer and explanation

Concise answer

The values checked are 8, then 1, then 5.

Why this is correct

At index 0, 8 is not equal to 5. At index 1, 1 is not equal to 5. At index 2, 5 matches the target, so 2 is printed and the break statement ends the search.

Use the concise answer above for a quick check, then reveal the full model answer and detailed explanation.

Common mistake

No common mistake is linked to this question yet.

Related flashcards

No flashcards are published for this page yet.

Related practice questions