This set covers the whole searching, sorting and files module: linear search, one sorting pass, records, end-of-file loops and edge-case testing. All data is fictional.
Questions run from easier to harder. Draw a trace table, write your answer, then open the working. The restricted pseudocode trace trainer can check a trace, and the mistake log and retest queue is the place to record slips.
Questions 1 to 3 use this array (positions 1 to 6) and the search from the linear search lesson:
| Position | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Codes | 14 | 9 | 27 | 9 | 31 | 5 |
Found ← FALSE
Index ← 1
WHILE Index <= 6 AND Found = FALSE
IF Codes[Index] = Target THEN
Found ← TRUE
ELSE
Index ← Index + 1
ENDIF
ENDWHILE
Questions
1. Target = 9. Give the final Index, Found and the number of comparisons.
Show answer
Pass 1: Codes[1] = 14, no, Index becomes 2. Pass 2: Codes[2] = 9, match, Found = TRUE. Index = 2, Found = TRUE, 2 comparisons.
2. Target = 40. Give the final Index, Found and the number of comparisons.
Show answer
No item matches, so all six are checked and Index increases after each one. Index = 7, Found = FALSE, 6 comparisons. The loop ends because Index <= 6 is false.
3. The Index ← Index + 1 line is moved outside the IF, to run after it. With Target = 27, what final Index is produced and why is it wrong?
Show answer
The match happens at Index = 3, but the increment still runs, so Index ends at 4. Codes[4] is 9, not 27, so the reported position is wrong.
4. A different list is [3, 8, 8, 1]. A linear search for 1 stops at the first match. How many comparisons are made?
Show answer
Compare 3, 8, 8, then 1 at position 4. 4 comparisons.
5. Trace one bubble-sort pass on [4, 7, 2, 9, 1]. Give the list afterwards and the number of swaps.
Show answer
4 and 7: no swap. 7 and 2: swap, giving 4, 2, 7, 9, 1. 7 and 9: no swap. 9 and 1: swap, giving 4, 2, 7, 1, 9. Result [4, 2, 7, 1, 9], 2 swaps. The largest value, 9, is last.
6. Data[1] = 5 and Data[2] = 8. A student swaps with Data[1] ← Data[2] then Data[2] ← Data[1]. What are the final values?
Show answer
After line one, Data[1] = 8. Line two copies Data[1], now 8, into Data[2]. Final: Data = [8, 8]. The 5 is lost because no temporary variable held it.
7. One pass is run on [1, 2, 3] with the flag method. What is Swapped afterwards, and what does it show?
Show answer
No pair is out of order, so no swap happens. Swapped = FALSE, showing the list is already sorted.
8. The line Zara,11A,64 is split at commas. Give the three fields and the score plus 6.
Show answer
Fields: Zara, 11A, 64. The score converted to a number is 64, and 64 + 6 = 70.
9. A fixed-width line Omar 09C055 uses widths 6, 3 and 3. Give the three fields and the score as a number.
Show answer
Characters 1 to 6: Omar . Characters 7 to 9: 09C. Characters 10 to 12: 055, which as a number is 55.
10. A file Marks.txt holds the lines 6, 10, 14, 2. Using the totalling loop from the end-of-file lesson, give Total, Count and the average.
Show answer
Reading 6, 10, 14, 2 gives totals 6, 16, 30, 32. The body runs four times. Total = 32, Count = 4, average = 32 / 4 = 8.
11. The same program runs on an empty file. What goes wrong, and how do you guard against it?
Show answer
The loop body never runs, so Count = 0. Then Total / Count divides by zero. Guard with IF Count = 0 THEN OUTPUT "No data" ELSE OUTPUT Total / Count ENDIF.
12. An algorithm finds the highest score and how many times it occurs. Trace it on [6, 6, 2, 6, 6].
Show answer
Start Max = 6, Times = 1. Item 2 is 6, equal, so Times = 2. Item 3 is 2, no change. Item 4 is 6, Times = 3. Item 5 is 6, Times = 4. Output 6, 4.
If you got these wrong
| What went wrong | Go to |
|---|---|
Wrong final Index, comparison count or missing target (1 to 4) | Trace a linear search |
| Wrong list after a pass, lost value in a swap (5 to 7) | Explain a sorting pass |
| Wrong field, text compared as a number (8, 9) | Read a record |
| Wrong total, loop that never ends, empty file (10, 11) | Handle end-of-file |
| Duplicates mis-counted, no test for the edge case (11, 12) | Check empty and duplicate data |
If the same row keeps coming up, our teachers can build extra practice around it in online one-to-one Computer Science tuition. Our Python reasoning sandbox is also useful for checking a short Python version of any trace.