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

Form 2 Asas Sains Komputer practice

You know the idea of tracing and want fresh programs to try on your own.

These eight original questions practise the two Form 2 Asas Sains Komputer skills: tracing a program, then choosing test inputs. Write every answer in a table before opening it.

They follow the Form 2 Asas Sains Komputer guide. All programs are invented for practice and are not taken from any school or official paper, and the keyword style may differ from your textbook.

Questions 1 to 4: tracing programs

1. Trace a price rule

INPUT n
total ← n × 3
IF total > 20 THEN
    total ← total − 5
END IF
PRINT total

Give the output for n = 4, n = 9 and n = 7.

Answer

n = 4: total is 12, which is not above 20, so the program prints 12. n = 9: total is 27, which is above 20, so it becomes 22 and prints 22. n = 7: total is 21, above 20, so it becomes 16 and prints 16.

Check by a second route: the rule is “3 times n, then take 5 off only if the result passes 20”. 3 × 7 = 21 passes by 1, so 21 − 5 = 16.

Correction hint: test the IF with the value of total, not with n. Compare with the canteen trace in the Form 2 guide.

2. Say what a program does

INPUT a
INPUT b
IF a > b THEN
    PRINT a − b
ELSE
    PRINT b − a
END IF

Trace (a, b) = (5, 8), (8, 5) and (6, 6). In one sentence, say what the program computes.

Answer

(5, 8): 5 > 8 is false, so ELSE prints 8 − 5 = 3. (8, 5): 8 > 5 is true, so it prints 8 − 5 = 3. (6, 6): 6 > 6 is false, so ELSE prints 6 − 6 = 0.

The program prints the gap between the two numbers, always as a positive number or zero.

Correction hint: when a and b are equal, the condition is false, so the ELSE branch runs. That is correct here because the answer is 0 either way.

3. Find the first wrong step

A pupil wants to swap two numbers.

x ← 4
y ← 9
x ← y
y ← x
PRINT x
PRINT y

Trace it. Which line is the first wrong step, and how can it be repaired?

Answer
after line x y
1 4 ?
2 4 9
3 9 9
4 9 9

The program prints 9 and 9. Line 3 is the first wrong step: x ← y overwrites the 4 before it is saved, so the 4 is lost.

Repair with a spare variable: t ← x, then x ← y, then y ← t. After those lines x is 9 and y is 4, so the swap works.

Correction hint: a copy destroys what was in the target. Save the value first.

4. Trace two conditions together

INPUT temp
INPUT raining
IF temp > 30 AND raining = "N" THEN
    PRINT "Go to the pool"
ELSE
    PRINT "Stay in"
END IF

Give the output for (33, “N”), (33, “Y”) and (28, “N”).

Answer

(33, “N”): 33 > 30 is true and raining is “N”, so both parts are true and it prints “Go to the pool”. (33, “Y”): the temperature part is true but raining is “Y”, so AND fails and it prints “Stay in”. (28, “N”): 28 > 30 is false, so AND fails and it prints “Stay in”.

Correction hint: AND needs both parts true. One false part sends the program to ELSE.

Questions 5 to 8: choosing tests and repairing

5. Find the missing branch

A program should print Low for 10 litres or less, Medium for 11 to 20 litres and High for more than 20.

INPUT litres
IF litres <= 10 THEN
    PRINT "Low"
ELSE IF litres > 20 THEN
    PRINT "High"
END IF

Choose four inputs that between them expose the fault. State the expected and the actual output for each.

Answer
input expected actual
10 Low Low
11 Medium nothing
20 Medium nothing
21 High High

Inputs 11 and 20 expose the missing Medium branch. Inputs 10 and 21 check the boundaries on each side.

Repair: add ELSE PRINT “Medium” just before END IF, so every value that fits neither test prints Medium.

Correction hint: add one test for each branch and one for the gap between branches.

6. Pick the revealing input

Pupils aged 18 or older may register. The program is:

INPUT age
IF age > 18 THEN
    PRINT "Allowed"
END IF

Which of the inputs 10, 25, 40 and 18 exposes the fault, and why do the others not?

Answer

Only 18 exposes it. Expected “Allowed”, but 18 > 18 is false, so the program prints nothing.

The inputs 10, 25 and 40 agree with the intended rule (10 is under 18 and prints nothing, 25 and 40 print the message), so they sit on the same side as correct answers and show no difference.

Repair: change > to >= .

Correction hint: when a rule says “or older” or “up to”, always test the number named in the rule.

7. Count the tests needed

INPUT stars
IF stars = 5 THEN
    PRINT "Excellent"
ELSE IF stars = 4 THEN
    PRINT "Good"
ELSE IF stars = 3 THEN
    PRINT "Fair"
END IF

The intended rule also prints “Needs work” for 1 or 2 stars. What is the smallest number of tests that reaches every outcome, and which inputs would you use?

Answer

Four tests: 5, 4, 3 and one of 1 or 2. The program has three branches plus the case where nothing matches, so four paths exist.

Input 2 prints nothing, but the intended output is “Needs work”. That shows the missing branch. Repair with a final ELSE PRINT “Needs work”.

Correction hint: count paths first, then pick one input for each.

8. Harder task: change a program, then retest

INPUT distance
INPUT bag
fee ← 5
IF distance > 10 THEN
    fee ← fee + 3
END IF
IF bag = "Y" THEN
    fee ← fee + 2
END IF
PRINT fee

(a) Trace (8, “N”), (12, “N”), (12, “Y”) and (10, “Y”). (b) Rewrite the program so that the bag charge applies only when distance is over 10, then trace (12, “Y”) and (8, “Y”).

Answer

(a) (8, “N”) prints 5. (12, “N”) prints 5 + 3 = 8. (12, “Y”) prints 5 + 3 + 2 = 10.

(10, “Y”) prints 5 + 2 = 7, because 10 > 10 is false.

(b) Put the bag test inside the first IF:

fee ← 5
IF distance > 10 THEN
    fee ← fee + 3
    IF bag = "Y" THEN
        fee ← fee + 2
    END IF
END IF
PRINT fee

(12, “Y”) prints 10. (8, “Y”) prints 5, because the whole block is skipped.

Check: the new program gives (10, “Y”) as 5, which differs from the old 7. That is the test input that shows the two versions are different.

Correction hint: an IF inside an IF runs only when the outer condition is true. Redo the trace with a column for each condition.

If you got these wrong

Fresh programs are what test tracing, and a teacher in one-to-one Asas Sains Komputer lessons can bring new ones, watch where your trace table goes wrong and fix that habit first.

Common questions

Are these questions from school or official papers?

No. Every program and question here is original practice written for this site. Use them to rehearse tracing and testing, then follow your teacher's exercises and any computer room work.

Do I need a computer to do these questions?

No. Use paper and a pencil, and write a trace table for each program. Writing every value down by hand is the skill being practised.

What should I do when my final output is wrong?

Compare your table with the worked one row by row and find the first line where a value differs. Fix the habit that caused that one line, then retrace the whole program.

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.