Past paper – A Level Computer Science (old syllabus, to 2021) (9608)
Cambridge International AS & A Level · 9608
Computer Science (old syllabus, to 2021) · Paper 42
Oct/Nov 2021 · 9608/42/O/N/21
Answer on paper, open each mark scheme and give yourself marks, then open the Summary.
Question text
1
An array, NumberArray, stores 100 integer values. The array needs to be sorted into ascending
numerical order.
(a) Describe how an insertion sort will sort the data in NumberArray.
…
…
…
…
…
…
…
…
…
… [4]
(b) Another type of sorting algorithm is a bubble sort.
The procedure Bubble() takes an array as a parameter. It performs a bubble sort on the
array. The sorting algorithm stops as soon as all the elements are in ascending order.
Complete the procedure Bubble().
PROCEDURE Bubble(BYREF NumberArray : ARRAY[0 : 99] OF INTEGER)
DECLARE Outer : INTEGER
DECLARE Swap : BOOLEAN
DECLARE Inner : INTEGER
DECLARE Temp : INTEGER
LENGTH(NumberArray) - 1
Outer
REPEAT
…
Inner
FALSE
Swap
REPEAT
IF NumberArray[Inner] > NumberArray[Inner + 1]
THEN
NumberArray[Inner]
Temp
NumberArray[Inner + 1]
NumberArray[Inner]
Temp
NumberArray[Inner + 1]
…
Swap
ENDIF
Inner + 1
Inner
UNTIL Inner = …
Outer - 1
Outer
UNTIL Swap = … OR Outer = …
ENDPROCEDURE
[5]
Question text
2
Complete the JSP structure diagram for the following pseudocode procedure.
PROCEDURE Calculate()
INPUT Number1
INPUT Number2
INPUT Command
IF Command = 1
THEN
Function1(Number1, Number2)
Value
ELSE
IF Command = 2
THEN
Function2(Number1, Number2)
Value
ELSE
Function3(Number1, Number2)
Value
ENDIF
ENDIF
OUTPUT Value
ENDPROCEDURE
JSP structure diagram
Calculate
[4]
Question text
3
A user has to choose a new password to create an account. It is recommended that the password
has at least two of the following elements:
•
upper-case letter
•
numeric character
•
symbol.
The system outputs:
•
"Strong" if there are at least two of the elements
•
"Medium" if there is only one of the elements
•
"Weak" if there are none of the elements.
Complete the following decision table for the password system described.
Rules
One or more
upper-case
N
Y
N
Y
N
Y
N
Y
letters
One or more
Conditions
numeric
N
N
Y
Y
N
N
Y
Y
characters
One or more
N
N
N
N
Y
Y
Y
Y
symbols
Strong
Actions
Medium
Weak
[3]
Question text
4
Each node of a binary tree is a record. Each record has a left pointer, an integer data value
between 0 and 100 inclusive, and a right pointer.
For example:
Item
Example data
LeftPointer
2
Data
34
RightPointer
3
(a) Write pseudocode to declare the record with the identifier Node.
…
…
…
…
…
…
… [2]
(b) Write pseudocode to declare a new node, Node100, and assign 100 to its data value, 1 to
the left pointer and 4 to the right pointer.
…
…
…
…
…
…
… [3]
(c) The ordered binary tree is stored as a 1D global array named BinaryTree of type Node.
RootNode and FreePointer are declared as global variables.
A null pointer is represented by –1.
The current state of the binary tree is shown in the following table:
RootNode
Index
LeftPointer
Data
RightPointer
0
[0]
1
23
3
FreePointer
6
[1]
-1
5
2
[2]
-1
8
4
[3]
5
100
-1
[4]
-1
9
-1
[5]
-1
88
-1
[6]
-1
null
-1
[7]
-1
null
-1
(i)
State the purpose of the free pointer.
…
… [1]
(ii)
Identify an appropriate integer value to represent null data.
… [1]
(iii)
Draw the current state of the binary tree.
[2]
(iv)
The procedure AddData():
•
takes the node to be added to the tree as a parameter
•
finds the location for the node to be stored
•
stores the node in the next free array index
•
stores –1 in the new node’s LeftPointer and RightPointer
•
updates the pointers in the other nodes
•
updates FreePointer.
Complete the pseudocode for the procedure AddData().
PROCEDURE AddData(NewNode)
…
BinaryTree[FreePointer]
BinaryTree[FreePoi
Question text
5
Study the following recursive pseudocode algorithm.
FUNCTION Recursive(Num1, Num2 : INTEGER) RETURNS INTEGER
IF Num1 > Num2
THEN
RETURN 10
ELSE
IF Num1 = Num2
THEN
RETURN Num1
ELSE
RETURN Num1 + Recursive(Num1 * 2, Num2)
ENDIF
ENDIF
ENDFUNCTION
(a) The function is called as follows:
Recursive(1, 15)
Dry run the function and complete the trace table. Give the final return value.
Trace table:
Num1
Num2
Function call
Return value
Final return value …
Working …
…
…
…
…
[4]
(b) Rewrite the function Recursive() in pseudocode, using an iterative algorithm.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
6
Details of errors generated in a program are stored in a stack.
Details of each error are stored in a record structure, Error.
(a) State which error will be the first retrieved from the stack.
…
… [1]
(b) The stack is implemented as a 1D array with the identifier ErrorArray.
The pointer LastItem stores the position of the last error in the array.
(i)
The function, AddItemToStack, takes the next error, the array, and pointer as
parameters.
If the stack is full, the function returns FALSE; otherwise it adds the error to the stack,
changes the pointer’s value and returns TRUE.
Complete the following pseudocode for the function AddItemToStack.
FUNCTION AddItemToStack(BYREF ErrorArray : ARRAY[0 : 99] OF Error,
BYREF LastItem : INTEGER,
BYVALUE Error1 : Error) RETURNS BOOLEAN
IF LastItem = …
THEN
RETURN …
ELSE
…
ErrorArray[LastItem + 1]
…
LastItem
RETURN …
ENDIF
ENDFUNCTION
[4]
(ii)
Explain the reasons why ErrorArray and LastItem are passed by reference, but
Error1 is passed by value.
…
…
…
…
…
… [3]
(iii)
The function RemoveItem takes the next error from the stack and returns it.
If there are no errors in the stack, it returns the global record NullError.
Complete the pseudocode algorithm RemoveItem.
FUNCTION RemoveItem(BYREF ErrorArray : ARRAY[0 : 99] OF Error,
BYREF LastItem : INTEGER) RETURNS Error
DECLARE ItemToRemove : Error
IF …
THEN
RETURN …
ELSE
ErrorArray[ … ]
ItemToRemove
LastItem - 1
LastItem
RETURN …
ENDFUNCTION
[3]
(iv)
The errors that have been processed are stored in a global queue, ErrorComplete.
The function Enqueue adds a record to ErrorComplete:
Enqueue(ErrorToAdd)
Enqueue() returns TRUE if the record is successfully added to the queue, and returns
FALSE if the queue is full.
The procedure RunError() should:
•
remove a record from the stack using the function RemoveItem()
•
output an appropriate message if there were no records in the stack
•
if an error record is returned, add the record to the queue using the function
Enqueue()
•
if the record is added to the queue, output an appropriate message
•
if the record is not added to the queue, output an appropriate message.
Complete the pseudocode procedure RunError().
PROCEDURE RunError(BYREF ErrorComplete : ARRAY[0 : 99] OF Error,
BYREF ErrorArray : ARRAY[0 : 99] OF Error)
…
…
Question text
7
A treasure box is hidden within a computer game.
The box has a code that needs to be entered to allow the user into the box. The box contains up
to 10 objects that are defined as being of the class FieldObject. The definition for the class Box
is:
Box
// small, medium or large
Size : STRING
// the 10 items the box holds
Contents : ARRAY[0 : 9] OF FieldObject
// the code to unlock the box
Lock : STRING
// the strength of the box
Strength : INTEGER
// decreases by 1 each time an
// incorrect code is entered
// instantiates an object of the Box
Constructor()
// class and assigns initial values
// to the attributes
// checks if the code is correct to
Unlock()
// unlock the box
// returns the array
GetContents()
// sets the size of the box
SetSize()
// sets the contents of the box
SetContents()
// sets the lock code
SetLock()
// sets the strength
SetStrength()
(a) The constructor creates a new instance of a box. It takes the size of the box, one item of
content and the lock code as parameters. The strength is initialised to 100.
Write program code to create the class and constructor for Box.
Do not write the code for Unlock() or any of the set or get methods. Use the constructor for
your chosen language.
Programming language …
Program code …
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
(b) The player inputs the code to unlock the box. Each time they enter an incorrect code, the
strength of the box decreases by 1. If the strength of the box becomes 0, the box automatically
unlocks.
The class Box has a method Unlock() that:
•
takes the
Question text
8
A game stores details about characters.
A declarative programming language is used to represent the following knowledge base:
01 hair(blonde).
02 hair(black).
03 hair(red).
04 face(glasses).
05 face(moustache).
06 face(beard).
07 person(ismail).
08 person(anisha).
09 person(kim).
10 person(kyle).
11 has(kyle, glasses).
12 has(kyle, beard).
13 has(anisha, red).
14 has(kyle, black).
These clauses have the following meaning:
Clause
Explanation
01
Hair can be blonde
04
Glasses can be on the face
08
Anisha is a person
12
Kyle has a beard
13
Anisha has red hair
A person, X, is a selected person if they have black hair and either a moustache or a beard.
Write a rule to represent this condition.
SelectedPerson(X)
IF
…
…
…
… [2]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of the University of