Skip to content
SPM Tuition
Advanced programming practice practice

Advanced programming practice with explained answers

You have read the lessons and now want fresh questions that make you trace and test.

These eight original questions get harder as you go. Write a trace table for each one before you open the answer, and assume arrays start at index 0.

They test the four skills in advanced programming practice. Use the timed original practice session builder if you want a repeatable session length.

Questions

Question 1. The array nums = [4, 8, 15, 16, 23, 42]. What is nums[3], and what is the last valid index?

Answer

Indexes count from 0, so nums[0] = 4, nums[1] = 8, nums[2] = 15 and nums[3] = 16. The array has 6 elements, so the last valid index is 5, which holds 42.

Lesson: working with arrays and structured data.

Question 2. Trace this code for scores = [12, 7, 15, 9]. What is printed?

count = 0
FOR i = 0 TO 3
    IF scores[i] > 10 THEN
        count = count + 1
    ENDIF
NEXT i
PRINT count
Answer
i scores[i] > 10? count
0 12 Yes 1
1 7 No 1
2 15 Yes 2
3 9 No 2

The program prints 2, the number of scores above 10.

Question 3. Each item in a list of records has name and price. The prices are Pen 2, Book 18, Ruler 3. What does this print?

FOR i = 0 TO 2
    IF item[i].price < 5 THEN
        PRINT item[i].name
    ENDIF
NEXT i
Answer

The condition is true for Pen (2) and Ruler (3), and false for Book (18). The output is Pen then Ruler, in index order.

Question 4. Use a linear search to find 15 in [9, 3, 15, 8, 15]. Which index is reported, and how many comparisons are made?

Answer

The search compares 9 (no), 3 (no), 15 (yes) and stops. It reports index 2 after 3 comparisons. The second 15 at index 4 is never reached.

Lesson: tracing searching and sorting algorithms.

Question 5. Trace a binary search for 8 in the sorted array [2, 5, 8, 11, 14, 17, 20], indexes 0 to 6. Show low, high and mid at each step.

Answer
Step low high mid data[mid] Decision
1 0 6 3 11 11 > 8, so high = 2
2 0 2 1 5 5 < 8, so low = 2
3 2 2 2 8 Found at index 2

Three comparisons find the value.

Question 6. Show the array after each pass when bubble sort arranges [3, 1, 2] in ascending order. How many swaps are made?

Answer

Pass 1: compare 3 and 1, swap, giving 1, 3, 2. Compare 3 and 2, swap, giving 1, 2, 3.

Pass 2: compare 1 and 2, no swap. Nothing changed, so the list is sorted.

After pass 1 the array is 1, 2, 3, and the total is 2 swaps.

Question 7. Classify each fault as syntax, runtime or logic.

  • (a) A loop meant to add the numbers 1 to 5 prints 10, because it starts at 0 and stops before 5. The expected total is 15.
  • (b) A line reads PRNT total.
  • (c) The program divides by a count that is 0 when the list is empty.
Answer
  • (a) Logic error. The code runs and gives a wrong result, because it uses the wrong bounds.
  • (b) Syntax error. PRNT is not valid, so the program will not run.
  • (c) Runtime error. The code is valid but dividing by zero stops the program for that input.

Lesson: debugging syntax, logic and runtime errors.

Question 8. A program accepts a password length of 8 to 12 characters inclusive and prints “OK” if it is valid, “Reject” otherwise. Write a test table with expected outputs that includes boundaries.

Answer
Length Type Expected output
10 Normal OK
8 Boundary OK
12 Boundary OK
7 Boundary Reject
13 Boundary Reject
0 Invalid Reject

The four boundary rows would catch a wrong > or >= in the condition. Expected outputs were worked out from the rule, not from the code.

Lesson: creating a test table with expected outputs.

If you got these wrong

If you want a teacher to go through your traces with you, see online one-to-one Computer Science tuition.

Common questions

Should I use a computer to check my answers?

Trace by hand first, because the exam is on paper. Then run the code and compare. If the two disagree, find the first step where your trace and the computer differ, and learn from that step.

How should I use the answers?

Cover the answer, write your own trace table, then compare. Check the column of variable values, not only the final output. A right answer from a wrong trace will fail you on the next question.

What language are the questions in?

Short pseudocode, with arrays starting at index 0 unless a question says otherwise. The reasoning applies in any language, but check your school's pseudocode style.

If the same kind of question keeps going wrong, one-to-one Computer Science lessons let a teacher watch your trace being built and stop you at the first wrong step.

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