Skip to content
SPM Tuition
Form 3 Asas Sains Komputer guide practice

Form 3 Asas Sains Komputer practice

You understand the two searches and want fresh lists and programs to test yourself.

These eight original questions practise comparing algorithm traces and repairing logic errors. Draw a table for each one and write the answer before opening it.

They go with the Form 3 Asas Sains Komputer guide. The lists and programs are invented for practice, are not from any school or official paper, and may use different keywords from your textbook. Count a comparison each time the target is checked against one list item.

Questions 1 to 4: comparing traces

Search the list 14, 6, 21, 9, 30 for 9, and then for 10. How many comparisons does each search take?

Answer

For 9: compare with 14, 6, 21 and 9. It is found at position 4 after 4 comparisons. For 10: compare with all 5 items, find no match, and stop after 5 comparisons.

Check: a miss always costs one comparison per item, so a 5-item list gives 5.

Correction hint: do not stop counting at position 3 just because the target is near. Count the checks, not the position of the target in your head.

The sorted list is 3, 8, 12, 17, 21, 26, 30, 35, 41 in positions 1 to 9. Trace a binary search for 26 and compare its comparisons with a linear search.

Answer

Positions 1 to 9: the middle is position 5, which holds 21. Since 26 is larger, keep positions 6 to 9. The middle is position 7, which holds 30.

Since 26 is smaller, keep position 6. Position 6 holds 26, which is found after 3 comparisons.

Linear search checks 3, 8, 12, 17, 21, 26, which takes 6 comparisons. Binary search needs half as many here.

Correction hint: when the range has an even number of positions, round the middle down and say so. Practise with the trace in the Form 3 guide.

3. Bubble sort by hand

Sort 5, 2, 4, 1 into ascending order with bubble sort. Show the list after each pass and count the swaps.

Answer

Pass 1: compare 5 and 2, swap, giving 2, 5, 4, 1. Compare 5 and 4, swap, giving 2, 4, 5, 1. Compare 5 and 1, swap, giving 2, 4, 1, 5.

After pass 1 the list is 2, 4, 1, 5 with 3 swaps.

Pass 2: 2 and 4 stay, 4 and 1 swap, giving 2, 1, 4, 5. That is 1 swap. Pass 3: 2 and 1 swap, giving 1, 2, 4, 5.

That is 1 swap. The total is 3 + 1 + 1 = 5 swaps.

Check another way: count the pairs in the wrong order in 5, 2, 4, 1. They are (5,2), (5,4), (5,1), (2,1) and (4,1), which is 5, and each swap fixes exactly one pair.

Correction hint: after each pass the largest remaining item sits at the end. If yours does not, you skipped a comparison.

4. Compare two sorts

Sort the same list, 5, 2, 4, 1, with selection sort. How many swaps does it use, and why does it use fewer than bubble sort?

Answer

Pass 1: the smallest item is 1, at position 4. Swap it with position 1, giving 1, 2, 4, 5. Pass 2: the smallest of 2, 4, 5 is 2, already in place.

Pass 3: the smallest of 4, 5 is 4, already in place.

Selection sort uses 1 swap, compared with 5 for bubble sort. Selection sort searches for the smallest item and moves it once, whereas bubble sort moves an item one place at a time by swapping neighbours.

Correction hint: describe the steps, not only the totals. “Bubble swaps neighbours, selection swaps once per pass” is the explanation the question wants.

Questions 5 to 8: logic errors and tests

5. Wrong loop limit

This program should print the average of the four items 6, 8, 10 and 12.

total ← 0
FOR i ← 1 TO 3
    total ← total + item[i]
NEXT i
PRINT total / 4

Trace it, name the first wrong step, correct it and record a test.

Answer

The loop runs for i = 1, 2 and 3, so total becomes 6, then 14, then 24. The program prints 24 ÷ 4 = 6, but the average is (6 + 8 + 10 + 12) ÷ 4 = 36 ÷ 4 = 9.

The first wrong step is the loop limit: FOR i ← 1 TO 3 skips item 4. Change it to FOR i ← 1 TO 4.

input expected before after
6, 8, 10, 12 9 6 9

Correction hint: when a loop reads a list, make the upper limit equal to the number of items.

6. Starting value problem

This program should print the largest of four temperatures.

largest ← 0
FOR i ← 1 TO 4
    IF item[i] > largest THEN
        largest ← item[i]
    END IF
NEXT i
PRINT largest

Trace it on −3, −1, −8, −5, correct it and choose a test that proves the fix.

Answer

No item is above 0, so largest stays 0 and the program prints 0. The correct answer is −1.

The starting value 0 is the first wrong step. Start with the first item instead: largest ← item[1]. Then the trace gives −3, then −1, then stays −1, so it prints −1.

The test must use all-negative data such as −3, −1, −8, −5: expected −1, before 0, after −1. A test with 4, 9, 2, 7 gives 9 both before and after, so it would not show the fix.

Correction hint: ask what happens when every item is below the starting value.

7. A search that forgets

found ← "N"
FOR i ← 1 TO 5
    IF item[i] = target THEN
        found ← "Y"
    ELSE
        found ← "N"
    END IF
NEXT i
PRINT found

The list is 4, 9, 7, 2, 5 and the target is 9. Trace the program, correct it and say which target would hide the fault.

Answer

i = 1: 4 is not 9, so found is “N”. i = 2: 9 matches, so found is “Y”. i = 3, 4 and 5: no match, so the ELSE sets found back to “N”. The program prints “N” although 9 is in the list.

The fault is the ELSE, which wipes out an earlier match. Remove the ELSE branch so found changes only to “Y”. Then the trace ends with “Y”.

A target of 5, the last item, hides the fault because the last check matches and the program prints “Y” before and after the fix. Use a target in the middle, like 9, to expose it.

Correction hint: a loop that changes a flag every time can undo its own work. Change the flag only when something is found.

8. Harder task: when is binary search not much better?

A sorted list holds the multiples of 5 from 5 to 75, which is 15 items. Count the comparisons for linear and binary search when the target is 60, and then when it is 25. What do the results show?

Answer

The item in position i is 5 × i, so 60 is in position 12 and 25 is in position 5.

Target 60: linear needs 12 comparisons. Binary checks position 8 (40), keeps positions 9 to 15, checks position 12 (60) and stops. That is 2 comparisons.

Target 25: linear needs 5 comparisons. Binary checks position 8 (40), keeps 1 to 7, checks position 4 (20), keeps 5 to 7, checks position 6 (30), keeps position 5, and checks position 5 (25). That is 4 comparisons.

For a target near the start, binary search saves only 1 comparison, so its advantage depends on where the target sits. Count the steps for the list you are given.

Correction hint: redo the binary trace and write the range of positions after every check. A missing range is where most counting slips happen.

If you got these wrong

  • Questions 1 and 2: read the search trace in the Form 3 guide and write the range after each check.
  • Questions 3 and 4: redo the sorts with the list written out after every swap.
  • Questions 5 to 8: go back to the pass-mark program in the guide, then revisit Form 2 practice on missing branches.

If you can run the two searches but struggle to explain them in words, online Asas Sains Komputer tuition lets you explain aloud to a teacher who then points out which step you skipped.

Common questions

Are these questions from school or official papers?

No. Every list, program and question here is original practice written for this site. Use them to rehearse the skills, then follow your teacher's work and the tasks set at school.

Should I learn bubble sort and selection sort by heart?

Learn what each step does rather than the words. Bubble sort swaps neighbours that are out of order. Selection sort finds the smallest remaining item and swaps it into place. If you can trace either, you can explain it.

What counts as a comparison when I count search steps?

Count each time the target is checked against one list item. A found item counts as a comparison too. State your rule at the start of the answer so the count is clear.

Want to work through this topic with a teacher? We can explain the steps, look at your attempted questions and help you decide what to practise next.

If you have a question or a piece of working, you can share it.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.