Skip to content
Book 2: Fundamental Problem-solving and Programming Skills

Book 2 · Chapter 0

How Paper 2 works

Exam craftExam craftB2·0

This 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.

Open in the ebook

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)
The guide's own functions. Notice MID(Name, 1, 5) doing the job other languages give to LEFT. The guide has no LEFT.

Output

11
Hamza
Iq
Iqbal
Qw
27
Run and checked before printing
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: ", InRange
RAND(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
Run and checked before printing

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
Passport check A: constants, the six types, and every operator. / 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
Run and checked before printing
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
Passport check B: a 1D array, a 2D array and a record. Notice [row, column], the dot for a field, and a whole record copied in one assignment.

Output

20 X Saddar
Run and checked before printing

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"
ENDCASE
Passport check C: a nested IF and a CASE with ranges and OTHERWISE. Only the first matching CASE clause runs.

Output

Pass
Band B
Run and checked before printing
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
Passport check D: FOR with a negative STEP, REPEAT and WHILE. The lines beginning › are the values typed in.

Output

10
7
4
1
› 4
› 7
› 0
Total 11
Left 2
Run and checked before printing
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) * 2
Passport check E: a procedure with BYREF parameters (BYREF carries on from X to Y, so both swap; CALL it) and a function (use its value; never CALL it).

Output

8 3
16
Run and checked before printing
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"
Passport check F: WRITE starts a new file, APPEND adds after the old lines, READ gets them back until EOF. Every file is closed.

Output

1: Mon 4
2: Tue 6
3: Wed 5
Run and checked before printing

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 inside a function: the count is shown on the screen, but nothing is given back to the line that called it. The interpreter refuses the function.

Output

ERROR: Line 1: FUNCTION CountChar never executes RETURN — a function must RETURN a value of type INTEGER
Run and checked before printing
FUNCTION Twice(Number : INTEGER) RETURNS INTEGER
   RETURN Number * 2
ENDFUNCTION

CALL Twice(21)
CALL is for procedures. A function's value must be used. Assign it, output it or test it.

Output

ERROR: Line 5: Twice is a FUNCTION — do not CALL it; use its value in an expression: Result ← Twice(…) (pseudocode guide §8.2)
Run and checked before printing
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')
A counter that is never set to 0. Pseudocode variables do not start at 0, so the first 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 ""
Run and checked before printing
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')
The Mark Ladder's answer, run on three test strings. One is the empty string, where the loop body never runs and 0 comes back.

Output

3
1
0
Run and checked before printing
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")
The faded twin, finished. Notice ' '. One space between single quotes is a CHAR.

Output

3
0
Run and checked before printing

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")
The second Mark Ladder's answer, tested on one valid code and three invalid ones, one for each rule.

Output

TRUE
FALSE
FALSE
FALSE
Run and checked before printing
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("")
The faded twin, finished. The empty string is rejected first, so TRUE means every character really was checked.

Output

TRUE
FALSE
FALSE
Run and checked before printing

The Misconception Museum: habits from other languages

Short code B2·0·16

DECLARE Score : INTEGER
Score ← 5
IF Score == 5 THEN
   OUTPUT "Five"
ENDIF
Exhibit 1: ==.

Output

ERROR: Line 3: Comparison uses a single = in pseudocode, not ==
Run and checked before printing
DECLARE Total : INTEGER
Total ← 0
Total += 5
OUTPUT Total
Exhibit 2: +=.

Output

ERROR: Line 3: There is no += in pseudocode — write X ← X + value
Run and checked before printing
DECLARE Name : STRING
Name ← "Ayesha"
OUTPUT LEN(Name)
Exhibit 3: LEN.

Output

ERROR: Line 3: There is no function called LEN — write LENGTH(<string>)
Run and checked before printing
DECLARE Mark : INTEGER
Mark ← 55
IF Mark >= 70 THEN
   OUTPUT "A"
ELSEIF Mark >= 50 THEN
   OUTPUT "B"
ENDIF
Exhibit 4: ELSEIF.

Output

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)
Run and checked before printing
DECLARE Mark : INTEGER
Mark ← 55
IF Mark >= 70 THEN
   OUTPUT "A"
ELSE
   IF Mark >= 50 THEN
      OUTPUT "B"
   ELSE
      OUTPUT "C"
   ENDIF
ENDIF
The same choice in guide form: ELSE, then a new IF inside it, each IF with its own ENDIF.

Output

B
Run and checked before printing
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 Index
Exhibit 5: EXIT FOR.

Output

ERROR: Line 9: There is no EXIT — make the loop condition FALSE, or RETURN from a function
Run and checked before printing
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 ", Index
Stopping early in guide form: a conditional loop with a BOOLEAN flag. The loop test does the stopping.

Output

TRUE at 2
Run and checked before printing
DECLARE First : STRING
DECLARE Last : STRING
First ← "Ali"
Last ← "Khan"
OUTPUT First + " " + Last
Exhibit 6: + to join text.

Output

ERROR: Line 5: + adds numbers — to join text use & (First & " "), or OUTPUT the values separated by commas
Run and checked before printing
DECLARE First : STRING
DECLARE Score : INTEGER
DECLARE Message : STRING
First ← "Ali"
Score ← 7
Message ← First & " scored " & NUM_TO_STR(Score)
OUTPUT Message
Joining text in guide form: & between STRINGs, and NUM_TO_STR (from the insert) to turn a number into text first.

Output

Ali scored 7
Run and checked before printing
DECLARE Answer : CHAR
Answer ← 'N'
IF Answer != 'Y' THEN
   OUTPUT "Stopping"
ENDIF
Exhibit 7: !=.

Output

ERROR: Line 3: Not-equal is <> in pseudocode, not !=
Run and checked before printing

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")
Part (b), answered from the plan. ExtractID() is the part (a) function, used here exactly as the stem defines it. The test file holds four bookings, three by M0042.

Output

3
1
0
Run and checked before printing

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.

Ask Z about How Paper 2 works
Buy ZAK's books — WhatsApp the books desk: Buy the booksPrint or ebook