This set covers the whole programming structure module: data types, local and global scope, parameters, returned values, and keeping logic apart from input and output. All programs and values are invented.
Questions run from easy to harder. Write your answer first, then open the working. Trace tables are shown in full so you can compare each row. The restricted pseudocode trace trainer is useful for the trace questions, the safe Python reasoning sandbox lets you check results, and the mistake log and retest queue is the place to record slips.
Questions
1. Choose the most suitable data type for each: (a) number of seats in a hall, (b) a temperature of 31.5, (c) a ticket code “0045”, (d) a single answer ‘Y’ or ‘N’, (e) whether it is raining.
Show answer
(a) INTEGER, a whole-number count. (b) REAL, it has a fractional part. (c) STRING, because it is never calculated with and the zeros at the start must be kept. (d) CHAR, one character. (e) BOOLEAN, only TRUE or FALSE.
2. A program stores the result of 15 / 4. State the value and the data type needed.
Show answer
15 / 4 = 3.75. The result has a fractional part, so it needs REAL. An INTEGER variable could not hold 3.75.
3. State the output.
Count ← 10
PROCEDURE Change()
DECLARE Count : INTEGER
Count ← 99
ENDPROCEDURE
CALL Change()
OUTPUT Count
Show answer
The procedure declares its own local Count and sets that to 99. The global Count is a separate variable and stays 10. The output is 10.
4. Count is declared in the main program. Temp is declared inside the procedure Calc. State whether each is local or global, and whether the main program can output Temp after Calc has run.
Show answer
Count is global. Temp is local to Calc. The main program cannot output Temp, because a local variable stops existing when the procedure ends.
5. Write a function Area that takes two REAL parameters Length and Width and returns their product as a REAL. State the value of Area(4.5, 2).
Show answer
FUNCTION Area(Length : REAL, Width : REAL) RETURNS REAL
RETURN Length * Width
ENDFUNCTION
Area(4.5, 2) returns 4.5 × 2 = 9.0.
6. Trace the function and state the value returned by RepeatAdd(6, 4).
FUNCTION RepeatAdd(A : INTEGER, B : INTEGER) RETURNS INTEGER
DECLARE Result : INTEGER
Result ← 0
FOR I ← 1 TO B
Result ← Result + A
NEXT I
RETURN Result
ENDFUNCTION
Show answer
| I | Result |
|---|---|
| start | 0 |
| 1 | 6 |
| 2 | 12 |
| 3 | 18 |
| 4 | 24 |
The function returns 24, which is 6 × 4.
7. This function should return 1 + 2 + 3 for SumTo(3). State what it actually returns, and correct it.
FUNCTION SumTo(N : INTEGER) RETURNS INTEGER
DECLARE Total : INTEGER
Total ← 0
FOR I ← 1 TO N
Total ← Total + I
RETURN Total
NEXT I
ENDFUNCTION
Show answer
RETURN is inside the loop, so on the first pass Total becomes 0 + 1 = 1 and the function ends. It returns 1.
Correction: move RETURN Total to after NEXT I. Then Total goes 1, 3, 6 and the function returns 6.
8. The function below reduces a price by a percentage. State the value of Discount(80, 25), then explain what happens if the arguments are swapped to Discount(25, 80).
FUNCTION Discount(Price : REAL, Percent : REAL) RETURNS REAL
RETURN Price - Price * Percent / 100
ENDFUNCTION
Show answer
Discount(80, 25): 80 × 25 / 100 = 20, and 80 − 20 = 60.
Discount(25, 80): Price is 25 and Percent is 80. 25 × 80 / 100 = 20, and 25 − 20 = 5. Arguments match parameters by position, so swapping them gives a wrong answer with no error message.
9. Write a function ToFahrenheit that takes a REAL Celsius value and returns Celsius × 9 / 5 + 32. Give the result for 30 and for 100.
Show answer
FUNCTION ToFahrenheit(Celsius : REAL) RETURNS REAL
RETURN Celsius * 9 / 5 + 32
ENDFUNCTION
For 30: 30 × 9 = 270, 270 / 5 = 54, 54 + 32 = 86. For 100: 100 × 9 = 900, 900 / 5 = 180, 180 + 32 = 212. The function has no INPUT or OUTPUT, so the main program would handle those.
10. State the output.
FUNCTION Score(Correct : INTEGER, Wrong : INTEGER) RETURNS INTEGER
RETURN Correct * 3 - Wrong
ENDFUNCTION
A ← Score(8, 2)
B ← Score(5, 5)
OUTPUT A + B
Show answer
Score(8, 2) = 8 × 3 − 2 = 24 − 2 = 22, so A = 22. Score(5, 5) = 15 − 5 = 10, so B = 10. The output is 22 + 10 = 32.
11. For each task, say whether a procedure or a function is more suitable and why: (a) display a welcome line, (b) work out a bill including an 8% service charge and give the total back to the caller.
Show answer
(a) A procedure: it only does something and no value comes back. (b) A function: the caller needs the total, so it uses RETURN. The service charge is added inside the function and the main program decides how to show the result.
12. State the output. Explain why the second call does not start from 0.
DECLARE Count : INTEGER
Count ← 0
FUNCTION Next(Step : INTEGER) RETURNS INTEGER
Count ← Count + Step
RETURN Count
ENDFUNCTION
DECLARE A : INTEGER
DECLARE B : INTEGER
A ← Next(2)
B ← Next(3)
OUTPUT A + B
Show answer
Count is global, so changes survive between calls. First call: Count = 0 + 2 = 2, returns 2, so A = 2. Second call: Count = 2 + 3 = 5, returns 5, so B = 5. Output is 2 + 5 = 7.
This works but makes the function depend on a global, which is the habit the module advises against. A cleaner design would pass the current count in as a parameter.
If you got these wrong
Match each error type to a lesson, repeat that lesson’s self-check, then retry the question a few days later.
| Error | Go to |
|---|---|
| Wrong type, especially codes stored as numbers (1, 2) | Choose an appropriate data type |
| Output or visibility of a local or global variable (3, 4, 12) | Local and global scope |
| Parameter order or function headers (5, 8, 11) | Build a subroutine with clear parameters |
| Wrong value after tracing, early RETURN (6, 7, 10) | Trace a returned value |
| Mixing calculation with input or output (9, 11) | Separate logic from input and output |
If your errors are spread across several lessons, a one-to-one teacher on our Computer Science tuition service can help you prioritise.