EXAM QUESTIONS · AS LEVEL 9618
Programming Exam-Style Questions with Answers (A Level 9618)
Original exam-style questions on Cambridge pseudocode, built-in functions, selection with CASE, loops, procedures and functions, BYVAL and BYREF parameters, records and file handling for Cambridge AS & A Level Computer Science 9618 (Paper 2). There are 26 marks in total. Try each question on paper first, then press Show answer to see the mark scheme. Answers that can be calculated mark themselves.
The variable Word contains “Algorithm”. State the value returned by each expression.
- LEFT(Word, 3) [1]
- RIGHT(Word, 4) [1]
- MID(Word, 4, 3) [1]
- LENGTH(Word) [1]
Show answer
- “Alg” [1]
- “ithm” [1]
- “ori” (start at character 4, take 3 characters) [1]
- 9 [1]
State the value of each expression.
- INT(7.9) [1]
- 23 MOD 4 [1]
- 23 DIV 4 [1]
Show answer
- 7 (INT removes the fractional part) [1]
- 3 [1]
- 5 [1]
Rewrite this nested IF statement using a CASE structure.
IF Grade = "A"
THEN
OUTPUT "Excellent"
ELSE
IF Grade = "B"
THEN
OUTPUT "Good"
ELSE
OUTPUT "Keep going"
ENDIF
ENDIFShow answer
CASE OF Grade "A" : OUTPUT "Excellent" "B" : OUTPUT "Good" OTHERWISE : OUTPUT "Keep going" ENDCASE
- 1 mark: CASE OF Grade
- 1 mark: “A” and “B” options with correct outputs
- 1 mark: OTHERWISE with “Keep going”
- 1 mark: ENDCASE
State the values of N and K after this algorithm has run.
N ← 1 K ← 0 REPEAT N ← N * 3 K ← K + 1 UNTIL N > 100
Show answer
- N = 243 [1] (3, 9, 27, 81, 243)
- K = 5 [1]
Consider this procedure and main program.
PROCEDURE Change(BYVAL A : INTEGER, BYREF B : INTEGER) A ← A * 2 B ← B + A ENDPROCEDURE X ← 3 Y ← 5 CALL Change(Y, X) OUTPUT Y, " ", X
State the output and explain your answer.
Show answer
- Output: 5 13 [1]
- Y is passed BYVAL, so A is a copy: doubling A to 10 does not change Y, which stays 5. [1]
- X is passed BYREF, so B refers to X: B ← 3 + 10 changes X to 13. [1]
Write pseudocode to declare a record type Student with fields Name (string), Age (integer) and Paid (Boolean), and then declare an array of 30 students.
Show answer
TYPE Student DECLARE Name : STRING DECLARE Age : INTEGER DECLARE Paid : BOOLEAN ENDTYPE DECLARE Class : ARRAY[1:30] OF Student
- 1 mark: TYPE Student … ENDTYPE
- 1 mark: three fields with correct data types
- 1 mark: array of 30 elements of type Student
A text file Scores.txt stores one integer score on each line. Write pseudocode to read every score from the file and output the highest score.
Show answer
DECLARE Line : STRING
DECLARE Score, Highest : INTEGER
Highest ← -1
OPENFILE "Scores.txt" FOR READ
WHILE NOT EOF("Scores.txt")
READFILE "Scores.txt", Line
Score ← STR_TO_NUM(Line)
IF Score > Highest
THEN
Highest ← Score
ENDIF
ENDWHILE
CLOSEFILE "Scores.txt"
OUTPUT "Highest score: ", Highest- 1 mark: OPENFILE … FOR READ and CLOSEFILE
- 1 mark: loop until EOF
- 1 mark: READFILE inside the loop (and conversion to a number)
- 1 mark: compares each score with the highest so far and updates it
- 1 mark: highest initialised before the loop and output after it
Give two reasons why a programmer would use a function rather than repeating the same code several times.
Show answer
Any two from:
- The code only needs to be written and tested once.
- Easier to maintain: a change only has to be made in one place.
- The program is shorter and easier to read.
- The function can be reused in other programs / added to a library.
- Different programmers can work on different functions.
Frequently asked questions
What is the difference between BYVAL and BYREF?
BYVAL passes a copy of the value, so changes inside the procedure do not affect the original variable. BYREF passes a reference (address), so changes inside the procedure change the original variable.
Are these past paper questions?
No. These are original exam-style questions written by a Computer Science teacher in the Cambridge style.
Revise the topic: 9618 programming notes · How to write Cambridge pseudocode · Coding Lab · All exam-style questions