Programming Constructs and Operators: Computer Science 0478 (Cambridge O Level / IGCSE)
Syllabus 8.1 · Strand 8 Programming
- Questions
- 10
- Total marks
- 50
- Tier mix
- 10 Core
0 of 10 questions completed
Syllabus coverage
- 8.1 10 questions completed
Every program, however complex, is built from a small set of constructs, and syllabus 8.1 is where they are introduced. A variable holds a value that can change while a program runs and a constant holds one that cannot, and every value must be given a data type: integer, real, char, string or Boolean, so the program knows how to store and process it. Input and output move data between the user and the program, and sequence simply runs instructions in the order they are written.
Selection lets a program choose between paths using IF or CASE statements, while iteration repeats a block of code using count-controlled, pre-condition or post-condition loops, and the two combine constantly in tasks like totalling a set of values or counting how many meet a condition. String handling adds operations such as finding a string’s length, extracting a substring, and converting to upper or lower case, remembering that the first character may be position zero or one depending on the language. Arithmetic operators (including MOD and DIV), relational operators for comparisons, and logical operators (AND, OR, NOT) combine these pieces into the conditions and expressions every algorithm depends on, including statements nested up to three levels deep.
The exam-style questions below are original, written to match this syllabus objective, and each is followed by a full worked solution so you can check your method step by step.
Question 1
A conference organiser uses this pseudocode to work out how many minibuses are needed to transport delegates to a venue. Each minibus seats exactly 9 people.
DECLARE Delegates : INTEGER
DECLARE FullBuses : INTEGER
DECLARE Remaining : INTEGER
Delegates ← 47
FullBuses ← Delegates DIV 9
Remaining ← Delegates MOD 9
OUTPUT FullBuses, " full minibuses and ", Remaining, " delegates left over"
What is output when this algorithm is run?
Question 2
A mobile network calculates the extra cost of a customer's data top-up using this pseudocode.
Data is charged in complete blocks of 500 MB, worked out using the DIV operator, and a
CASE OF statement then selects the cost band for that number of blocks.
DECLARE DataUsedMB : INTEGER
DECLARE Blocks : INTEGER
DECLARE Cost : REAL
DataUsedMB ← 1350
Blocks ← DataUsedMB DIV 500
CASE OF Blocks
0 : Cost ← 0.00
1 : Cost ← 3.00
2 : Cost ← 5.50
OTHERWISE : Cost ← 5.50 + (Blocks - 2) * 2.00
ENDCASE
OUTPUT Cost
(a) State the value of Blocks after this algorithm executes, showing your working. [2]
(b) State the value output for Cost, and identify which branch of the CASE OF statement
is used to produce it. [2]
(c) A second customer uses 2870 MB of data in the same month. State the value output for
Cost for this customer, showing your working. [2]
Question 3
A weekend fundraiser records the donation made by each of 6 donors, together with whether the donation was made in cash. This pseudocode algorithm totals all of the donations, and also counts how many donations were both at least $20 and made in cash.
01 DECLARE Donation : REAL
02 DECLARE IsCash : BOOLEAN
03 DECLARE Total : REAL
04 DECLARE BigCashCount : INTEGER
05 DECLARE Counter : INTEGER
06 Total ← 0
07 BigCashCount ← 0
08 FOR Counter ← 1 TO 6
09 INPUT Donation
10 INPUT IsCash
11 Total ← Total + Donation
12 IF Donation >= 20 AND IsCash = TRUE THEN
13 BigCashCount ← BigCashCount + 1
14 ENDIF
15 NEXT Counter
16 OUTPUT Total
17 OUTPUT BigCashCount
The algorithm is run once, using this donation data, entered in the order shown:
| Donor | Donation ($) | IsCash |
|---|---|---|
| 1 | 15 | TRUE |
| 2 | 25 | TRUE |
| 3 | 30 | FALSE |
| 4 | 20 | TRUE |
| 5 | 50 | FALSE |
| 6 | 22 | TRUE |
(a) State the line number of the statement that performs totalling, and the line number of the statement that performs counting. Explain how you identified each one. [2]
(b) Complete a full trace table showing the value of Total and BigCashCount after each pass
of the loop, for the data given above. [4]
(c) State the values output for Total and BigCashCount when the algorithm finishes. [2]
Question 4
A banking app displays a customer's reference code on screen with only the last two characters visible; every character before that is replaced with an asterisk. This pseudocode algorithm produces that masked display, one character at a time, using a post-condition loop.
01 DECLARE Code : STRING
02 DECLARE Index : INTEGER
03 Code ← "48213"
04 Index ← 1
05 REPEAT
06 IF Index <= LENGTH(Code) - 2 THEN
07 OUTPUT "*"
08 ELSE
09 OUTPUT SUBSTRING(Code, Index, 1)
10 ENDIF
11 Index ← Index + 1
12 UNTIL Index > LENGTH(Code)
(a) State the value of LENGTH(Code) - 2 for Code = "48213", and state the smallest value
of Index for which the ELSE branch (line 8–9) is taken. [2]
(b) Complete a trace table showing the value of Index and the character output on each pass
of the loop, for Code = "48213". Hence state the complete sequence of characters displayed
on screen. [4]
(c) A different reference code, Code = "9", is used instead. State what is displayed on
screen when the algorithm executes with this value, showing your working. [2]
Question 5
A cinema's admission rule is: "A customer may watch a 12A-rated film if they are at least 12 years old, or if they are accompanied by an adult."
A program declares Age : INTEGER and AccompaniedByAdult : BOOLEAN for each customer.
Which pseudocode condition correctly represents this admission rule?
Question 6
A weather station technician enters a sequence of temperature readings, in degrees Celsius, one
at a time. The value -999 is used as a sentinel value to signal that there is no more data,
and is never itself a real temperature reading. This pseudocode algorithm totals the real
readings entered and counts how many of them there were, stopping as soon as the sentinel value
is read.
01 DECLARE Reading : INTEGER
02 DECLARE Total : INTEGER
03 DECLARE Count : INTEGER
04 Total ← 0
05 Count ← 0
06 INPUT Reading
07 WHILE Reading <> -999 DO
08 Total ← Total + Reading
09 Count ← Count + 1
10 INPUT Reading
11 ENDWHILE
12 OUTPUT Total
13 OUTPUT Count
(a) State whether this is a pre-condition loop or a post-condition loop, explaining your answer with reference to a specific line number. [2]
(b) The technician enters the readings 18, 22, -5, 31, -999, in that order. Complete a
trace table showing the value of Reading, Total and Count after each pass of the loop. [4]
(c) On a different day, the technician enters -999 as the very first reading (there is no
real data at all). State the values output for Total and Count in this case, explaining
why. [2]
Question 7
A training course grades each learner's final score, out of 100, using this pseudocode, with categories nested up to three levels deep.
DECLARE Score : INTEGER
DECLARE Grade : STRING
Score ← 68
IF Score < 40 THEN
Grade ← "Fail"
ELSE
IF Score < 60 THEN
Grade ← "Pass"
ELSE
IF Score < 75 THEN
Grade ← "Merit"
ELSE
Grade ← "Distinction"
ENDIF
ENDIF
ENDIF
OUTPUT Grade
What is output when this algorithm is run?
Question 8
An online store, GreenLeaf, builds a one-line order confirmation message by joining several
pieces of text together with the & operator. The store's name never changes while the
program runs, so it is declared as a constant rather than a variable.
CONSTANT StoreName = "GreenLeaf"
DECLARE CustomerName : STRING
DECLARE OrderNumber : INTEGER
DECLARE Message : STRING
CustomerName ← "Amara"
OrderNumber ← 4521
Message ← StoreName & " order confirmed for " & CustomerName & ", reference #" & OrderNumber
OUTPUT Message
(a) State one difference between StoreName, declared with CONSTANT, and CustomerName,
declared with DECLARE, in terms of how their values may change while the program runs. [2]
(b) State the exact value of Message that is output, showing how each piece is joined by the
& operator. [3]
(c) A second order is placed by a customer named "Femi", with OrderNumber 7803. State the
exact value of Message output for this order, showing your working. [3]
Question 9
A library system stores four pieces of data about each book: how many pages it has, its
average reader rating out of 5 (for example 4.5), a single-letter genre code (for example
'F' for fiction), and whether it is currently on loan.
Which set of DECLARE statements uses the most appropriate data type for each of these four
pieces of data?
Question 10
A firework display uses a digital countdown board. This pseudocode counts down from 15 to 3 in steps of 3, displaying each number and totalling how many numbers are displayed.
01 DECLARE Count : INTEGER
02 DECLARE Displays : INTEGER
03 Displays ← 0
04 FOR Count ← 15 TO 3 STEP -3
05 OUTPUT Count
06 Displays ← Displays + 1
07 NEXT Count
08 OUTPUT Displays
(a) State the value of Count on the first pass of this loop, and the value of Count on the
final pass of this loop. [2]
(b) Complete a trace table showing the value of Count and Displays after each pass of the
loop. [4]
(c) A technician changes line 04 to FOR Count ← 15 TO 3 STEP -4. State the sequence of values
output for Count, and the final value of Displays, for this modified loop, showing your
working. [2]