Cambridge IGCSE / A-Level Computer Science
Paper 2 — Step-by-Step Solved Answers
All questions solved with full explanations
Q1 — Pseudocode, Data Types & Expressions
(a) Complete the table — Selection, Iteration, Subroutine
Each pseudocode example must be classified by whether it uses Selection, Iteration, and/or a
Subroutine (Procedure/Function). A single example can have more than one tick.
Pseudocode Example Selection Iteration Subroutine
FOR Index ← 1 TO 1 IF Safe[Index] =
TRUE THEN Flag[Index] ← 0 ENDIF NEXT ✔ ✔
Index
CASE OF Compound(3) ✔ ✔
REPEAT UNTIL AllDone() = TRUE ✔ ✔
WHILE Result[1] <> FALSE ✔
Explanation:
■ Example 1 (FOR … IF): Uses FOR loop (Iteration) + IF statement (Selection). No subroutine
call.
■ Example 2 (CASE OF Compound(3)): CASE is Selection; Compound(3) is a function call
(Subroutine).
■ Example 3 (REPEAT UNTIL AllDone()): REPEAT UNTIL is Iteration; AllDone() is a function
(Subroutine).
■ Example 4 (WHILE Result[1]): WHILE is Iteration only.
(b) Complete the table — Data Types
Identify the correct data type for each variable based on its example value.
Variable Example Data Value Data Type
Available TRUE Boolean
Booked "18/04/2021" String
Count 100 Integer
■ Available = TRUE/FALSE → Boolean
■ "18/04/2021" is enclosed in quotes → String (even though it looks like a date)
■ 100 is a whole number → Integer
(c) Evaluate each expression using the values from part (b)
Using: Available = TRUE, Booked = "18/04/2021", Count = 100
Expression 1: Available AND NOT(Index > 100)
Assume Index = 100 (or any reasonable test value ≤ 100):
Available = TRUE Index > 100 → 100 > 100 → FALSE NOT(FALSE) → TRUE TRUE
AND TRUE → TRUE
■ Result: TRUE
Expression 2: Index MOD 30
Index = 100 100 MOD 30 → 100 = 3 × 30 + 10 → remainder = 10
■ Result: 10
Expression 3: NUM_TO_STR(Index + "23")
Index is INTEGER (100), "23" is a STRING. You cannot add an integer
directly to a string — this is a type mismatch.
■ Result: ERROR (cannot concatenate/add Integer to String without conversion)
Q2 — Pseudocode Algorithm: Sum of Positive Integers
(a) Write pseudocode for the algorithm
The algorithm must: prompt and input 100 integers one at a time, sum only the positive ones (zero is
neither positive nor negative), and output the result.
Step-by-step reasoning:
■ 1. Declare variables: a counter for the loop, a variable for each input, and an accumulator for the
sum.
■ 2. Initialise the sum to 0.
■ 3. Loop exactly 100 times.
■ 4. Inside the loop: prompt the user, read the value, check if it is > 0, and if so add it to the sum.
■ 5. After the loop, output the sum.
Pseudocode Solution:
DECLARE Count : INTEGER DECLARE Number : INTEGER DECLARE Total : INTEGER
Total ← 0 FOR Count ← 1 TO 100 OUTPUT "Enter an integer: " INPUT Number IF
Number > 0 THEN Total ← Total + Number ENDIF NEXT Count OUTPUT "Sum of
positive integers = ", Total
Key points:
■ FOR loop runs exactly 100 times — matches the requirement precisely.
■ IF Number > 0 correctly excludes zero and negative numbers.
■ All variables declared with types (required by the question).
■ Total is initialised to 0 before the loop starts.
Q4 — File Handling: Writing Array Data to a Text File
Given: LogArray is a 1D array of 500 elements of type STRING.
Procedure LogEvents must append non-empty elements to the end of the existing text file [Link].
Unused array elements contain the value "Empty" and must NOT be written to the file.
Step-by-step reasoning:
■ 1. Open the file [Link] in APPEND mode (so existing content is preserved).
■ 2. Loop through all 500 elements of LogArray (index 1 to 500).
■ 3. For each element, check if it is NOT equal to "Empty".
■ 4. If valid, write the element to the file.
■ 5. Close the file after the loop.
Pseudocode Solution:
PROCEDURE LogEvents() DECLARE Index : INTEGER OPENFILE "[Link]" FOR
APPEND FOR Index ← 1 TO 500 IF LogArray[Index] <> "Empty" THEN WRITEFILE
"[Link]", LogArray[Index] ENDIF NEXT Index CLOSEFILE "[Link]"
ENDPROCEDURE
Key points:
■ APPEND mode adds data to the end of the file without deleting existing content.
■ "Empty" elements are filtered out using an IF condition inside the loop.
■ File is always closed after use to prevent data loss.
Q5 — Program Development Life Cycle
(a) Complete the table — Life Cycle Stage for each Activity
The Program Development Life Cycle (PDLC) stages are: Analysis, Design, Coding
(Implementation), Testing, Maintenance.
Activity Name of Life Cycle Stage
A compiler is used. Coding / Implementation
A program that has been released for general use is
Maintenance
modified.
The dry run method is used. Testing
The program structure is specified. Design
Explanation:
■ A compiler is used during Coding/Implementation — the written source code is compiled into
executable code.
■ Modifying a released program is Maintenance — fixing bugs or adding features post-release.
■ Dry run (tracing through code by hand) is a Testing technique.
■ Specifying program structure (data structures, modules, flowcharts) happens during Design.
(b) Method to continue testing the main program BEFORE errors in modules are corrected
Method:
■ Stub Testing (using Stub Modules / Stubs)
Description — How it works:
■ A stub is a simplified, dummy version of a module (procedure/function) that has not yet been
fully written or contains errors.
■ The stub has the same name and interface (parameters/return type) as the real module but
contains only minimal code — usually just returning a fixed/dummy value.
■ The main program calls the stub instead of the real module, so integration testing of the main
program's logic can proceed without waiting for the real module to be error-free.
■ Once the real modules are corrected, they replace the stubs and full integration testing
continues.
Note: Example: If TEST_AVG contains errors, a stub version would simply return a fixed value like 50,
allowing the main program to be tested for correct flow and logic.
Q6 — 2D Array Processing: Video-Conferencing Sound Samples
Given: Array Sample[6, 128] of type INTEGER — 6 rows (users), 128 columns (sound samples).
Procedure Mix() must: for each column, calculate the average of the 6 values (ignoring values ≤ 10),
then store the result in Result[column].
Step-by-step reasoning:
■ 1. Loop through each column (1 to 128).
■ 2. For each column, initialise a sum and a count of valid values.
■ 3. Loop through each row (1 to 6), check if the sample > 10.
■ 4. If valid, add to sum and increment count.
■ 5. Divide sum by count to get the average (only if count > 0 to avoid division by zero).
■ 6. Store the average in Result[column].
Pseudocode Solution:
PROCEDURE Mix() DECLARE Col : INTEGER DECLARE Row : INTEGER DECLARE Total :
INTEGER DECLARE Count : INTEGER FOR Col ← 1 TO 128 Total ← 0 Count ← 0
FOR Row ← 1 TO 6 IF Sample[Row, Col] > 10 THEN Total ← Total + Sample[Row,
Col] Count ← Count + 1 ENDIF NEXT Row IF Count > 0 THEN Result[Col] ←
Total DIV Count ELSE Result[Col] ← 0 ENDIF NEXT Col ENDPROCEDURE
Key points:
■ Nested FOR loops: outer loop iterates columns (1–128), inner loop iterates rows (1–6).
■ IF Sample[Row, Col] > 10 correctly ignores values of 10 or less.
■ Count tracks how many valid values exist to compute a correct average.
■ DIV gives integer division (appropriate since Result is INTEGER type).
■ Guard: IF Count > 0 prevents division by zero if all values in a column are ≤ 10.
End of Solved Paper | All answers follow Cambridge pseudocode conventions.