Book 2 · Chapter 0
How Paper 2 works
B2·0This chapter is in the ebook.
Read it in full on cswithzak.com. Every question has its mark scheme and every program is ready to run. The free practice for each section is below, with the short code the book prints beside it.
About this chapter
This chapter is about how the paper works rather than one syllabus section. Its companion material arrives with the chapter.
Paper 2 at a glance
Short code B2·0·10
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
The insert: built-in functions
Short code B2·0·11
DECLARE Name : STRING
Name ← "Hamza Iqbal"
OUTPUT LENGTH(Name)
OUTPUT MID(Name, 1, 5)
OUTPUT MID(Name, 7, 2)
OUTPUT RIGHT(Name, 5)
OUTPUT UCASE('q'), LCASE('W')
OUTPUT INT(27.9)MID(Name, 1, 5) doing the job other languages give to LEFT. The guide has no LEFT.Output
11 Hamza Iq Iqbal Qw 27
DECLARE Throw : INTEGER
DECLARE Count : INTEGER
DECLARE InRange : BOOLEAN
InRange ← TRUE
FOR Count ← 1 TO 1000
Throw ← INT(RAND(6)) + 1
IF Throw < 1 OR Throw > 6 THEN
InRange ← FALSE
ENDIF
NEXT Count
OUTPUT "Every throw from 1 to 6: ", InRangeRAND(6) gives a REAL from 0 up to (not including) 6. INT(…) + 1 turns it into a whole number from 1 to 6. A thousand throws, and every one is in range.Output
Every throw from 1 to 6: TRUE
The pseudocode passport I: data and expressions
Short code B2·0·12
// Passport check A: data and expressions
CONSTANT MaxSeats = 40
CONSTANT Fare = 2.50
DECLARE Seats : INTEGER
DECLARE Average : REAL
DECLARE Grade : CHAR
DECLARE Route : STRING
DECLARE Full : BOOLEAN
DECLARE Departs : DATE
Seats ← 37
Average ← Seats / 2
Grade ← 'B'
Route ← "R" & "12"
Full ← Seats >= MaxSeats
Departs ← 05/09/2027
OUTPUT Seats DIV 5, " remainder ", Seats MOD 5
OUTPUT Average, " ", Fare * 4
OUTPUT Route, " ", Grade, " ", Full
OUTPUT NOT Full AND Seats <> 0
OUTPUT "Departs ", Departs/ always gives a REAL; DIV and MOD give whole numbers.Output
7 remainder 2 18.5 10 R12 B FALSE TRUE Departs 05/09/2027
TYPE Stop
DECLARE Name : STRING
DECLARE Minutes : INTEGER
ENDTYPE
DECLARE Times : ARRAY[1:5] OF INTEGER
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
DECLARE First : Stop
DECLARE Spare : Stop
Times[1] ← 8
Board[2, 3] ← 'X'
First.Name ← "Saddar"
First.Minutes ← 12
Spare ← First
OUTPUT Times[1] + Spare.Minutes, " ", Board[2, 3], " ", Spare.Name[row, column], the dot for a field, and a whole record copied in one assignment.Output
20 X Saddar
The pseudocode passport II: control, modules and files
Short code B2·0·13
DECLARE Mark : INTEGER
Mark ← 64
IF Mark >= 50 THEN
IF Mark >= 70 THEN
OUTPUT "Merit"
ELSE
OUTPUT "Pass"
ENDIF
ELSE
OUTPUT "Retake"
ENDIF
CASE OF Mark
0 TO 49 : OUTPUT "Band C"
50 TO 79 : OUTPUT "Band B"
OTHERWISE : OUTPUT "Band A"
ENDCASEOutput
Pass Band B
DECLARE Index : INTEGER
DECLARE Total : INTEGER
DECLARE Guess : INTEGER
FOR Index ← 10 TO 1 STEP -3
OUTPUT Index
NEXT Index
Total ← 0
REPEAT
INPUT Guess
Total ← Total + Guess
UNTIL Guess = 0
OUTPUT "Total ", Total
WHILE Total > 9
Total ← Total - 9
ENDWHILE
OUTPUT "Left ", Total› are the values typed in.Output
10 7 4 1 › 4 › 7 › 0 Total 11 Left 2
PROCEDURE Swap(BYREF X : INTEGER, Y : INTEGER)
DECLARE Temp : INTEGER
Temp ← X
X ← Y
Y ← Temp
ENDPROCEDURE
FUNCTION Larger(A : INTEGER, B : INTEGER) RETURNS INTEGER
IF A > B THEN
RETURN A
ENDIF
RETURN B
ENDFUNCTION
DECLARE P : INTEGER
DECLARE Q : INTEGER
P ← 3
Q ← 8
CALL Swap(P, Q)
OUTPUT P, " ", Q
OUTPUT Larger(P, Q) * 2Output
8 3 16
DECLARE Line : STRING
DECLARE Count : INTEGER
OPENFILE "Log.txt" FOR WRITE
WRITEFILE "Log.txt", "Mon 4"
WRITEFILE "Log.txt", "Tue 6"
CLOSEFILE "Log.txt"
OPENFILE "Log.txt" FOR APPEND
WRITEFILE "Log.txt", "Wed 5"
CLOSEFILE "Log.txt"
Count ← 0
OPENFILE "Log.txt" FOR READ
WHILE NOT EOF("Log.txt")
READFILE "Log.txt", Line
Count ← Count + 1
OUTPUT Count, ": ", Line
ENDWHILE
CLOSEFILE "Log.txt"Output
1: Mon 4 2: Tue 6 3: Wed 5
How code is marked
Short code B2·0·14
FUNCTION CountChar(Text : STRING, Target : CHAR) RETURNS INTEGER
DECLARE Count : INTEGER
DECLARE Index : INTEGER
Count ← 0
FOR Index ← 1 TO LENGTH(Text)
IF MID(Text, Index, 1) = Target THEN
Count ← Count + 1
ENDIF
NEXT Index
OUTPUT Count
ENDFUNCTION
DECLARE Found : INTEGER
Found ← CountChar("banana", 'a')Output
ERROR: Line 1: FUNCTION CountChar never executes RETURN — a function must RETURN a value of type INTEGER
FUNCTION Twice(Number : INTEGER) RETURNS INTEGER
RETURN Number * 2
ENDFUNCTION
CALL Twice(21)Output
ERROR: Line 5: Twice is a FUNCTION — do not CALL it; use its value in an expression: Result ← Twice(…) (pseudocode guide §8.2)
FUNCTION CountChar(Text : STRING, Target : CHAR) RETURNS INTEGER
DECLARE Count : INTEGER
DECLARE Index : INTEGER
FOR Index ← 1 TO LENGTH(Text)
IF MID(Text, Index, 1) = Target THEN
Count ← Count + 1
ENDIF
NEXT Index
RETURN Count
ENDFUNCTION
OUTPUT CountChar("banana", 'a')Count + 1 has nothing to add to.Output
ERROR: Line 6: 'Count' has no value yet (DECLAREd on line 2) — assign it before using it; a variable does not start at 0 or ""
FUNCTION CountChar(Text : STRING, Target : CHAR) RETURNS INTEGER
DECLARE Count : INTEGER
DECLARE Index : INTEGER
Count ← 0
FOR Index ← 1 TO LENGTH(Text)
IF MID(Text, Index, 1) = Target THEN
Count ← Count + 1
ENDIF
NEXT Index
RETURN Count
ENDFUNCTION
OUTPUT CountChar("banana", 'a')
OUTPUT CountChar("Paper 2", 'p')
OUTPUT CountChar("", 'a')Output
3 1 0
FUNCTION CountSpaces(Sentence : STRING) RETURNS INTEGER
DECLARE Spaces : INTEGER
DECLARE Index : INTEGER
Spaces ← 0
FOR Index ← 1 TO LENGTH(Sentence)
IF MID(Sentence, Index, 1) = ' ' THEN
Spaces ← Spaces + 1
ENDIF
NEXT Index
RETURN Spaces
ENDFUNCTION
OUTPUT CountSpaces("read the stem twice")
OUTPUT CountSpaces("pseudocode")' '. One space between single quotes is a CHAR.Output
3 0
A function that tests: the second ladder
Short code B2·0·15
FUNCTION IsValidRoom(Code : STRING) RETURNS BOOLEAN
DECLARE Index : INTEGER
DECLARE Ch : STRING
IF LENGTH(Code) <> 5 THEN
RETURN FALSE
ENDIF
Ch ← MID(Code, 1, 1)
IF Ch < "A" OR Ch > "Z" THEN
RETURN FALSE
ENDIF
FOR Index ← 2 TO 5
Ch ← MID(Code, Index, 1)
IF Ch < "0" OR Ch > "9" THEN
RETURN FALSE
ENDIF
NEXT Index
RETURN TRUE
ENDFUNCTION
OUTPUT IsValidRoom("B0412")
OUTPUT IsValidRoom("B041")
OUTPUT IsValidRoom("b0412")
OUTPUT IsValidRoom("B04X2")Output
TRUE FALSE FALSE FALSE
FUNCTION IsAllDigits(Text : STRING) RETURNS BOOLEAN
DECLARE Index : INTEGER
DECLARE Ch : STRING
IF Text = "" THEN
RETURN FALSE
ENDIF
FOR Index ← 1 TO LENGTH(Text)
Ch ← MID(Text, Index, 1)
IF Ch < "0" OR Ch > "9" THEN
RETURN FALSE
ENDIF
NEXT Index
RETURN TRUE
ENDFUNCTION
OUTPUT IsAllDigits("2027")
OUTPUT IsAllDigits("20 27")
OUTPUT IsAllDigits("")TRUE means every character really was checked.Output
TRUE FALSE FALSE
The Misconception Museum: habits from other languages
Short code B2·0·16
DECLARE Score : INTEGER
Score ← 5
IF Score == 5 THEN
OUTPUT "Five"
ENDIF==.Output
ERROR: Line 3: Comparison uses a single = in pseudocode, not ==
DECLARE Total : INTEGER
Total ← 0
Total += 5
OUTPUT Total+=.Output
ERROR: Line 3: There is no += in pseudocode — write X ← X + value
DECLARE Name : STRING
Name ← "Ayesha"
OUTPUT LEN(Name)Output
ERROR: Line 3: There is no function called LEN — write LENGTH(<string>)
DECLARE Mark : INTEGER
Mark ← 55
IF Mark >= 70 THEN
OUTPUT "A"
ELSEIF Mark >= 50 THEN
OUTPUT "B"
ENDIFOutput
ERROR: Line 5: There is no ELSEIF in pseudocode — write ELSE on its own line, then a new IF … THEN … ENDIF inside it (each IF needs its own ENDIF)
DECLARE Mark : INTEGER
Mark ← 55
IF Mark >= 70 THEN
OUTPUT "A"
ELSE
IF Mark >= 50 THEN
OUTPUT "B"
ELSE
OUTPUT "C"
ENDIF
ENDIFOutput
B
DECLARE Names : ARRAY[1:4] OF STRING
DECLARE Index : INTEGER
Names[1] ← "Ali"
Names[2] ← "Sara"
Names[3] ← "Bilal"
Names[4] ← "Hina"
FOR Index ← 1 TO 4
IF Names[Index] = "Sara" THEN
EXIT FOR
ENDIF
NEXT IndexOutput
ERROR: Line 9: There is no EXIT — make the loop condition FALSE, or RETURN from a function
DECLARE Names : ARRAY[1:4] OF STRING
DECLARE Index : INTEGER
DECLARE Found : BOOLEAN
Names[1] ← "Ali"
Names[2] ← "Sara"
Names[3] ← "Bilal"
Names[4] ← "Hina"
Index ← 0
Found ← FALSE
WHILE Index < 4 AND NOT Found
Index ← Index + 1
IF Names[Index] = "Sara" THEN
Found ← TRUE
ENDIF
ENDWHILE
OUTPUT Found, " at ", IndexOutput
TRUE at 2
DECLARE First : STRING
DECLARE Last : STRING
First ← "Ali"
Last ← "Khan"
OUTPUT First + " " + Last+ to join text.Output
ERROR: Line 5: + adds numbers — to join text use & (First & " "), or OUTPUT the values separated by commas
DECLARE First : STRING
DECLARE Score : INTEGER
DECLARE Message : STRING
First ← "Ali"
Score ← 7
Message ← First & " scored " & NUM_TO_STR(Score)
OUTPUT Message& between STRINGs, and NUM_TO_STR (from the insert) to turn a number into text first.Output
Ali scored 7
DECLARE Answer : CHAR
Answer ← 'N'
IF Answer != 'Y' THEN
OUTPUT "Stopping"
ENDIF!=.Output
ERROR: Line 3: Not-equal is <> in pseudocode, not !=
Reading a scenario question
Short code B2·0·17
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
The long scenario question
Short code B2·0·18
FUNCTION CountBookings(MemberID : STRING) RETURNS INTEGER
DECLARE Line : STRING
DECLARE Total : INTEGER
Total ← 0
OPENFILE "Bookings.txt" FOR READ
WHILE NOT EOF("Bookings.txt")
READFILE "Bookings.txt", Line
IF ExtractID(Line) = MemberID THEN
Total ← Total + 1
ENDIF
ENDWHILE
CLOSEFILE "Bookings.txt"
RETURN Total
ENDFUNCTION
OUTPUT CountBookings("M0042")
OUTPUT CountBookings("M0107")
OUTPUT CountBookings("M9999")Output
3 1 0
Planning your time
Short code B2·0·19
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
Command-word coach
Short code B2·0·20
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
Past-paper practice
Short code B2·0·90
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
Workbook 2: How Paper 2 works
Short code W2·0 · Problem-solving and Programming Skills Workbook
The write-in workbook for this chapter has drills, exam-style practice, trace tables, fix-the-mistake and mark-it-yourself pages. Its full mark schemes and an interactive version arrive here with the chapter.


