Skip to content
SPM Tuition
Programming foundations practice

Programming foundations practice

You have read the lessons and want to see whether you can predict programs you have not seen.

These seven questions cover programming foundations. Attempt each on paper, then open the answer to compare.

For exam pace, set up a session with the timed original practice session builder.

Questions

Question 1. Choose a data type for each: (a) the number of books borrowed, (b) an average mark such as 72.5, (c) the class name “4 Amanah”, (d) whether a library card is valid.

Answer

(a) Integer. (b) Real. (c) String. (d) Boolean.

The average needs a decimal part, and the class name contains letters.

Question 2. Which of these should be a constant: the days in a week, the tickets bought in an order, a school pass mark of 40, a player’s current score?

Answer

The days in a week (7) and the school pass mark (40) should be constants because they are fixed. The tickets bought and the current score change, so they are variables.

Question 3. How many times does this loop body run, and what is the last value printed?

FOR i = 3 TO 11
  OUTPUT i
NEXT i
Answer

The passes are 11 − 3 + 1 = 9. The values printed are 3 to 11, so the last is 11.

Question 4. Trace and give the final x and count.

SET x = 1
SET count = 0
WHILE x < 50
  SET x = x * 2
  SET count = count + 1
END WHILE
Answer

x becomes 2, 4, 8, 16, 32 and 64, with count rising from 1 to 6. After x = 32 the check 32 < 50 is true, so one more pass makes x = 64. Then 64 < 50 is false. The final values are x = 64 and count = 6.

Question 5. What does this output for marks 75, 60 and 59?

IF mark >= 80 THEN
  OUTPUT "A"
ELSE IF mark >= 60 THEN
  OUTPUT "B"
ELSE
  OUTPUT "C"
END IF
Answer

For 75, the first test fails and 75 >= 60 passes, giving B. For 60, the second test passes on the boundary, giving B. For 59 both tests fail, giving C.

Question 6. Trace this and state the output.

FUNCTION triple(n)
  RETURN n * 3
END FUNCTION

SET a = 2
SET b = triple(a) + triple(a + 1)
OUTPUT a, b
Answer

triple(2) returns 6. triple(3) returns 9. So b = 6 + 9 = 15.

The variable a is unchanged. The output is 2 and 15.

Question 7. The values 5 and 8 are added in a program. What is the result if both are integers, and what if both are strings that are joined?

Answer

As integers, 5 + 8 = 13. As strings, joining gives “58”. The same digits give different results, which is why the data type must be decided before the operation.

If you got these wrong

Log your slips with the mistake log and paper-error review. For teacher support, see online one-to-one Computer Science tuition.

Common questions

Do I need to write code on a computer for these questions?

No. Every question is meant to be done on paper, the same way pseudocode is usually assessed. Working by hand also builds the habit of tracing carefully.

What should I do if I get a trace wrong?

Redraw the table and check the row where your answer first differs from the explanation. That row shows the exact misunderstanding, whether a type, a boundary or a parameter.

Are these questions from past papers?

No. They are original questions written for this site with every number checked by hand. They practise the same skills without copying any examination paper.

If the function or loop questions slowed you down, a one-to-one Computer Science teacher can practise just those structures with you until the tracing feels steady.

  • 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.