Skip to content
IGCSE·Tuition
Computer Science · Practice

Repetition and arrays: original mixed practice with explanations

Tracing is a skill you build by doing it on paper, so this set asks you to write the trace before you look.

On this page
  1. Warm-up: count-controlled loops
  2. Condition-controlled loops
  3. Repair and spot the fault
  4. Arrays and loops
  5. If you got these wrong

This set covers the skills in repetition and arrays: tracing count-controlled and condition-controlled loops, repairing off-by-one faults, and using arrays with a loop. Attempt each question on paper and write the trace table before you open the answer.

Questions 7 to 11 use the array Temp[1:6], which holds the readings 28, 31, 27, 33, 30, 33. Log your wrong answers in the mistake log.

Warm-up: count-controlled loops

Q1. What is output?

FOR i ← 1 TO 4
   OUTPUT i * i
NEXT i
Show answer

i takes 1, 2, 3, 4. The outputs are 1, 4, 9, 16.

Q2. How many times does the loop run, and what is Sum at the end?

Sum ← 0
FOR i ← 2 TO 10 STEP 2
   Sum ← Sum + i
NEXT i
Show answer

i takes 2, 4, 6, 8, 10, so the loop runs 5 times. Sum is 2, 6, 12, 20, 30. The final value is 30.

Condition-controlled loops

Q3. What are N and Count at the end?

N ← 100
Count ← 0
WHILE N > 10 DO
   N ← N DIV 3
   Count ← Count + 1
ENDWHILE
Show answer
TestN afterCount after
100 > 10 true331
33 > 10 true112
11 > 10 true33
3 > 10 falseends

N is 3 and Count is 3.

Q4. What is the value of X when the loop ends, and how many passes were made?

X ← 1
REPEAT
   X ← X * 2
UNTIL X > 50
Show answer

X becomes 2, 4, 8, 16, 32 (each is not > 50, so repeat), then 64 (64 > 50, stop). That is 6 passes and X is 64.

Repair and spot the fault

Q5. The task is to output 1 to 8. This outputs 1 to 7. Repair it.

Count ← 1
WHILE Count < 8 DO
   OUTPUT Count
   Count ← Count + 1
ENDWHILE
Show answer

When Count is 8, 8 < 8 is false and 8 is never output. Change the condition to WHILE Count <= 8. Then Count 1 to 8 pass the test and 8 values are output.

Q6. This loop never ends. Explain why, repair it, then state the final N and the number of passes for your repair.

N ← 10
WHILE N <> 0 DO
   N ← N - 3
ENDWHILE
Show answer

N takes 7, 4, 1, -2, -5 and so on. It skips over 0, so N <> 0 is always true. Repair: WHILE N > 0.

Trace: 10 > 0 true (N = 7), 7 > 0 true (N = 4), 4 > 0 true (N = 1), 1 > 0 true (N = -2), -2 > 0 false. That is 4 passes and N is -2.

Arrays and loops

Q7. Complete the code to count how many readings are above 30, then give the output.

Hot ← 0
FOR i ← 1 TO 6
   IF Temp[i] > 30
      THEN
         Hot ← Hot + 1
   ENDIF
NEXT i
OUTPUT Hot
Show answer

The readings above 30 are 31, 33 and 33. 30 is not above 30. Hot is 3.

Q8. Trace this and give the output.

Max ← Temp[1]
MaxPos ← 1
FOR i ← 2 TO 6
   IF Temp[i] > Max
      THEN
         Max ← Temp[i]
         MaxPos ← i
   ENDIF
NEXT i
OUTPUT MaxPos, Max
Show answer
iTemp[i]TestMaxMaxPos
start281
23131 > 28 true312
327false312
43333 > 31 true334
530false334
63333 > 33 false334

The output is 4, 33.

Q9. Write pseudocode to output the total and the average of all six readings. Then give both values, with the average to 1 decimal place.

Show answer
Total ← 0
FOR i ← 1 TO 6
   Total ← Total + Temp[i]
NEXT i
Average ← Total / 6
OUTPUT Total, Average

Total after each pass: 28, 59, 86, 119, 149, 182. The total is 182. The average is 182 / 6 = 30.33…, so 30.3 to 1 decimal place.

Q10. What is Temp[Temp[1] - 25]?

Show answer

Temp[1] is 28. 28 - 25 = 3. Temp[3] is 27.

Q11. A stock array Stock[1:4] starts as 5, 0, 3, 0. Give its contents after this code.

FOR i ← 1 TO 4
   IF Stock[i] = 0
      THEN
         Stock[i] ← 10
   ENDIF
NEXT i
Show answer

Positions 2 and 4 hold 0 and are replaced. The array becomes 5, 10, 3, 10.

If you got these wrong

Step through any of these in the pseudocode trace trainer or the Python reasoning sandbox. If one error type keeps returning, our online one-to-one Computer Science tuition can focus on exactly that skill.

Questions people ask

Should I run the code to check my answers?

Trace on paper first, then check with a tool. Running the code tells you the result but not why, and an exam asks you to explain each step. Use a sandbox only after your table is complete, and compare the two line by line.

What if my answer differs from the explanation?

Compare your trace table with the one shown, row by row. The first row that differs is where the error began. Note the type of error, such as wrong pass count or index for value, and use the routing list at the end.

Are these like exam questions?

They are original questions on the same skills: tracing, repairing and writing loops over arrays. They do not copy any past paper. Check your syllabus on the Cambridge page for the notation your exam year expects.

Sources

  1. Cambridge IGCSE Computer Science 0478 syllabus page

Updated:

Your next step

If the same kind of question keeps costing you a line of the trace, a one-to-one teacher can go through your working with you and find the habit behind the slip.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service