Data types, arrays and files – A Level Computer Science (old syllabus, to 2021) (9608)

118 questions · page 1 of 8

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
1
9608/21/O/N/21 · Question 3Oct/Nov 2021Paper 2117 marks
Loading question…
Question text

3
An aeroplane carries cases.
A case is not allowed on the aeroplane if:
•
the total number of cases previously checked in for a flight is 300 or more
•
the weight of a case exceeds the maximum of 50 kilograms.
The following statements apply:
•
Each flight is identified by a five-character string variable FlightNo
•
Each case is identified by a three-character string variable CaseID
•
The weight of each case is represented by a two-digit numeric string variable CaseWeight
A text file, HOLD-CARGO.txt, stores data for cases on all flights. The format of each line of the file
is:
<FlightNo><CaseID><CaseWeight>
(a) A procedure, CheckWeight(), is required as follows:
•
take the flight number as parameter
•
output the list of case IDs for the flight that are not allowed on the aeroplane.
Write pseudocode for the procedure CheckWeight().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [8]
(b) A decision is made to use consta

2
9608/21/O/N/21 · Question 4Oct/Nov 2021Paper 2111 marks
Loading question…
Question text

4
A willow tree is a type of tree that grows at an angle. A 1D array, TreeAngle, has 20 elements of
integer type. Each element stores the angle of a willow tree measured in degrees.
A procedure called SafetyCheck() is required to:
•
check each tree angle in the array and count the number of trees that exceed the safe limit of
36 degrees
•
determine whether the count exceeds a maintenance limit stored in the global variable
MainTrigger, in which case a maintenance visit is required
•
output a suitable message if the count is less than or equal to MainTrigger, otherwise
output a message showing the number of times the safe limit was exceeded as follows:
"Maintain 10 trees".
(a) The SafetyCheck() procedure is written in pseudocode.
Complete the following pseudocode.
Refer to the Appendix on pages 18–19 for a list of built-in pseudocode functions and operators.
PROCEDURE SafetyCheck()
DECLARE Count : INTEGER
DECLARE Index : INTEGER
CONSTANT TreeCount = 20
0
…
1 TO …
FOR Index
IF TreeAngle[Index] > …
THEN
Count + 1
Count
ENDIF
ENDFOR
IF … <= MainTrigger
THEN
OUTPUT "Maintenance not needed"
ELSE
OUTPUT "Maintain " & NUM_TO_STRING(Count) & " trees"
ENDIF
ENDPROCEDURE
[4]
(b) The TreeAngle array is changed to a 2D array that contains 20 rows and 2 columns of
integer numbers as follows:
•
The first column stores the integer reference number of the tree.
•
The second column stores the integer angle of the tree.
Study this example:
767 // tree reference
TreeAngle[15, 1]
12 // the angle of the tree
TreeAngle[15, 2]
A procedure, CheckTree(), is called with an integer parameter representing the tree’s
reference number.
The procedure will:
•
search the TreeAngle array for the reference number
•
when a match is found, prompt and input the angle of the tree, store the data in the
second dimension of the TreeAngle array, and then output a message if its safety
status has changed
•
when a match is not found, output the reference number and "No match".
Write program code for the procedure CheckTree().
Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…

3
9608/21/O/N/21 · Question 6Oct/Nov 2021Paper 2113 marks
Loading question…
Question text

6
A geocode is a string that specifies a geographical location.
The string consists of two alphanumeric characters which are followed by:
•
the character '+'
•
two more alphanumeric characters
•
a comma
•
a description of the location.
Here is an example of an email message that contains two geocodes. The first is for Coventry in
the UK and the second is for Cambridge in the UK.
"This is my previous address 65+ER,CoventryUK.
I have now moved to my new location at 64+AA,CambridgeUK."
(a) A programmer decides to store the geocodes in a 1D array called Location. The array
contains 10 000 elements and unused array elements are initialised with the string "22+VV".
Write pseudocode to declare and initialise the array Location.
…
…
…
…
…
…
…
… [3]
(b) The following example pseudocode uses the function FindGeoCodeIndex() to search the
array Location for the index position of a given geocode.
DECLARE CodePosition : INTEGER
FindGeoCodeIndex(GeoCode)
CodePosition
IF CodePosition <> -1
THEN
OUTPUT Geocode & " found at " & NUM_TO_STRING(CodePosition)
ENDIF
Complete the program flowchart to represent the algorithm only for the function
FindGeoCodeIndex(). Variable declarations are not required in program flowcharts.
START
END
[4]
(c) The function RetrieveCode() will extract a geocode from an email message.
Here is a summary of the design requirements for this function.
Parameters
Returns
Example
"This is my
Message
•
•
A string containing the
A string containing the
previous address
email message
geocode
65+ER,CoventryUK."
•
An integer value
RetrieveCode(Message, 29)
containing the start
returns "65+ER,CoventryUK"
position of the geocode
in the message string
Assume that:
•
the integer value points to the start of a valid geocode
•
if a valid geocode is not at the end of the email message, it will be followed by a space
or a full stop.
Write program code for the function RetrieveCode().
Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
Programming language …
Program code
…
…
…
…

Advertisement
4
9608/22/O/N/21 · Question 4Oct/Nov 2021Paper 2215 marks
Loading question…
Question text

4
A global 1D array Flower represents the flowers in a field and is defined as follows:
•
The array contains 20 elements of type integer.
•
Each element of the array contains a number associated with each flower.
(a) A procedure, InitialiseArray(), initialises the array Flower by setting the value of each
element to –1.
Write pseudocode for the procedure InitialiseArray().
…
…
…
…
…
…
…
…
… [4]
(b) A procedure called RandomPath() assigns values to the array Flower to store a path
through the flowers in the fields as follows:
•
the first flower to be visited is flower number 10
•
randomly select the next flower to be visited between 1 and 20
•
store the index of the next flower to be visited in the current element of the array Flower
if the flower has not been visited
•
visit each flower only once
•
repeat until the path has been completed through all flowers.
An element of –1 indicates that a flower has not been visited.
Write program code for the procedure RandomPath().
Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…

5
9608/22/O/N/21 · Question 6Oct/Nov 2021Paper 2213 marks
Loading question…
Question text

6
A geocode string consists of four characters that are followed by:
•
the character '+'
•
two more characters
•
a comma
•
a description of the location.
For example:
•
The geocode string for Cambridge, UK is "646A+6R,CambridgeUK".
•
The geocode string for Chicago, USA is "V9PG+3P,ChicagoUSA".
A program uses two 1D arrays to store data about the locations in which photographs were taken.
•
An array GeoCodeData contains a geocode string for each location. A geocode string is
added to this array if it does not exist in the array.
•
An array GeoCodeLog contains the first seven characters of the geocode string for each
photograph, followed by a space and the date when the photograph was taken. The date
format is DD/MM/YYYY.
For example, a photograph taken in Cambridge, UK on 21/12/2020 is represented as:
"646A+6R 21/12/2020"
Assume that:
•
both arrays contain 20 000 elements
•
new data is added to the next unused element
•
unused array elements are indicated by the string "AAAA+0A".
(a) A programmer has started to define the program module SearchLog().
Module
Description
•
takes the first seven characters of a geocode as a parameter
•
counts the number of times this parameter occurs in the array
GeoCodeLog
SearchLog()
•
extracts the date the last photograph was taken at the geocode location
•
returns a string formed by concatenating the count, a single space and
the date
Write pseudocode for the module SearchLog().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

6
9608/23/O/N/21 · Question 3Oct/Nov 2021Paper 2318 marks
Loading question…
Question text

3
The function AddressChecker() checks if an email address is in the format:
<user>@<host> • <domain>
The design requirements of the function are:
•
The <user> contains only lower-case characters.
•
The <domain> contains exactly three alphabetic characters.
AddressChecker() will use three functions as described in the following table.
Function
Description
Split()
•
Takes two parameters:
•
a STRING value containing the email address
•
a BOOLEAN value representing the required return value
•
Returns two different values:
•
a STRING containing <user> if the BOOLEAN parameter
value is TRUE
•
a STRING containing <domain> if the BOOLEAN
parameter value is FALSE
IsLowerCase()
•
Takes an element of the email address as a STRING
parameter
•
Returns a BOOLEAN value of TRUE if the string contains only
lower-case characters, otherwise returns FALSE
IsThreeLetter()
•
Takes the domain as a STRING parameter
•
Returns a BOOLEAN value of TRUE if the string consists of
exactly three alphabetic characters, otherwise returns FALSE
AddressChecker() will:
•
prompt and input an email address
•
check that the email address is valid
•
return the string "Invalid" if the email address does not meet the design requirements,
otherwise return the email address.
(a) Draw a program flowchart to represent the algorithm for the AddressChecker() function.
Variable declarations are not required in program flowcharts.
[6]
(b) The function AddressChecker() is changed to check a list of email addresses stored in a
text file.
The function’s header is changed as follows:
FUNCTION AddressChecker(Filename : STRING, StartLine : INTEGER,
LastLine : INTEGER) RETURNS BOOLEAN
Assume that:
•
The function is called with a valid filename of the text file.
•
The text file exists and contains at least one email address.
•
Each email address is stored in a separate line in the text file.
•
The function does not contain logical errors.
Give two reasons why the function may return an unexpected result.
Reason 1 …
…
Reason 2 …
…
[2]
(c) (i)
Explain what is meant by the term syntax error.
…
…
…
… [2]
(ii)
Give an example of a syntax error in program code.
Programming language …
Program code
…
…
…
… [1]
(d) The following

7
9608/23/O/N/21 · Question 4Oct/Nov 2021Paper 2316 marks
Loading question…
Question text

4
The following pseudocode contains a function that searches a text file for the string contained in
the parameter Match.
01 DECLARE MatchItems : ARRAY[1:100] OF STRING
10 FUNCTION Extract(FileName : STRING, Match : STRING,
Signal : INTEGER) RETURNS BOOLEAN
11
12
DECLARE FileLine : INTEGER
13
DECLARE Counter : INTEGER
14
DECLARE FileData, SubMatch : STRING
15
DECLARE Result : BOOLEAN
16
0
17
Counter
1
18
FileLine
FALSE
19
Result
20
21
OPENFILE FileName FOR READ
22
23
WHILE NOT EOF(FileName) AND FileLine <= 100
24
READFILE FileName, FileData
LEFT(FileData, LENGTH(Match))
25
SubMatch
26
IF SubMatch = Match
27
THEN
FileData
28
MatchItems[Counter]
Counter + 1
29
Counter
30
ENDIF
FileLine + 1
31
FileLine
32
ENDWHILE
33
34
CLOSEFILE FileName
35
IF Counter >= Signal
36
THEN
TRUE
37
Result
38
ENDIF
39
RETURN Result
40
41 ENDFUNCTION
(a) Complete the trace table when the function is called using this statement:
Extract("DATA.txt", "TG12367", 1)
Present
You may assume that the text file DATA.txt contains the following lines:
"XD43668#23/11/19#DSCP"
"TG12367#24/01/19#MAHA"
"HD44356#24/11/19#MAHA"
"TG12367#24/11/19#GHFI"
FileLine
Counter
LEFT(FileData,
SubMatch = Match
Result
LENGTH(Match))
[5]
(b) The function is changed as follows:
•
The statement on line 25 is changed to compare the last ten characters of FileData
and the parameter Match.
If the comparison is successful:
•
store the first seven characters of FileData in a global 1D array called Original
•
store the last four characters of FileData in a global 1D array called Backup
•
The count of the number of items stored in the array Backup is returned.
(i)
Rewrite the function header in pseudocode so that it meets the new requirements.
…
…
…
… [2]
(ii)
Rewrite the WHILE structure in pseudocode so that it meets the new requirements. The
WHILE structure is given here for reference.
23
WHILE NOT EOF(FileName) AND FileLine <= 100
24
READFILE FileName, FileData
LEFT(FileData, LENGTH(Match))
25
SubMatch
26
IF SubMatch = Match
27
THEN
FileData
28
MatchItems[Counter]
Counter + 1
29
Counter
30
ENDIF
FileLine + 1
31
FileLine
32
ENDWHILE
…
…
…
…
…
…
…
…
…
…

8
9608/23/O/N/21 · Question 5Oct/Nov 2021Paper 2318 marks
Loading question…
Question text

5
A geocode string contains four alphanumeric characters, followed by the character '+' and then
two alphanumeric characters.
For example, the geocode string for the Avocado Restaurant in Cambridge UK is:
"1DFG+9N"
A website allows a user to upload a review of a restaurant.
A programmer has defined the following two arrays:
Array
Description
GeoData
•
a global 1D array of data type STRING
•
stores the geocode for each restaurant
Example:
"1DFG+9N"
GeoData[1]
Review
•
a global 1D array of data type STRING
•
stores the following data items as a single element in the array:
•
the four-character user ID of the reviewer
•
the geocode of the restaurant
•
the text of the review
•
the hash symbol (#) separates the data items in each single element
Example of a review for the Avocado restaurant submitted by user ID JH76:
"JH76#1DFG+9N#Good restaurant"
Review[1000]
Assume that:
•
both arrays are initialised with an empty string
•
both arrays can store up to 20 000 elements
•
new reviews are added in the next unused element of the Review array.
(a) The programmer writes a function CheckReview().
The design requirements of the function are shown in the following table:
Description
Parameter
Returns
•
•
•
searches the
a STRING value
the comment from the review
GeoData array to
containing the
if the geocode exists in the
determine if the user’s
user ID of the
GeoData array
latest review contains
reviewer
•
a STRING value of "LOCATION
a listed geocode
NOT FOUND" if the geocode of
the user’s review does not exist
in the GeoData array
•
a STRING value of "NO
REVIEW" if no review exists for
the user ID
Write pseudocode to complete the function CheckReview().
DECLARE GeoData : ARRAY[1:20000] OF STRING
DECLARE Review : ARRAY[1:20000] OF STRING
FUNCTION CheckReview(UserID : STRING) RETURNS STRING
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

Advertisement
9
9608/41/O/N/21 · Question 1Oct/Nov 2021Paper 4137 marks
Loading question…
Question text

1
Sandy is writing a program to process data in a stack. The stack is implemented as a 1D array,
DataStack, which has up to 100 elements.
The function Push(Value) stores Value on the stack and returns TRUE if Value was added to
the stack, or FALSE if the stack is full.
The function Pop() returns the item at the top of the stack, or returns –1 if the stack is empty.
DataStack and TopPointer are declared as global.
(a) Show the state of DataStack and its pointer after the following functions are executed on
the current contents.
Pop()
Pop()
Push(19)
Pop()
Push(50)
TopPointer
Index
Data
3
[7]
[6]
[5]
[4]
[3]
8
[2]
6
[1]
20
[0]
10
[2]
(b) Write program code for the function Pop().
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
(c) Sandy has also used a queue in her program.
Describe the ways in which a queue differs from a stack.
…
…
…
…
…
… [2]

10
9608/41/O/N/21 · Question 3Oct/Nov 2021Paper 416 marks
Loading question…
Question text

3
The following pseudocode algorithm performs a binary search on the sorted array ThisArray.
The algorithm returns either the location of SearchItem in the array, or –1 if SearchItem is not
in the array.
The function DIV returns the integer value of the division, for example, 11 DIV 2 returns 5.
Complete the algorithm by writing the missing pseudocode statements.
FUNCTION BinarySearch(ThisArray[], LowerBound, UpperBound,
SearchItem : INTEGER) RETURNS INTEGER
DECLARE Flag : BOOLEAN
DECLARE Mid : INTEGER
-2
Flag
WHILE Flag <> -1
LowerBound + ((UpperBound – LowerBound) DIV 2)
Mid
IF … < …
THEN
RETURN …
ELSE
IF ThisArray[Mid] > SearchItem
THEN
Mid …
UpperBound
ELSE
IF ThisArray[Mid] < SearchItem
THEN
Mid …
LowerBound
ELSE
RETURN …
ENDIF
ENDIF
ENDIF
ENDWHILE
ENDFUNCTION
[6]

11
9608/41/O/N/21 · Question 6Oct/Nov 2021Paper 4115 marks
Loading question…
Question text

6
Kobi is writing an application that uses a record structure to store data.
(a) (i)
Describe what is meant by a record structure.
…
…
…
… [2]
(ii)
The record structure stores the unique ID number (a whole number), first name and last
name of a customer.
Write a pseudocode declaration for the record structure CustomerData.
…
…
…
…
…
…
… [2]
(b) Kobi’s application stores the records in a random access file.
The function StoreRecord():
•
takes a customer record as a parameter
•
uses the function CustomerHash() to calculate and return the hash value for its
parameter
•
stores the customer record in the returned hash value address.
Assume there are no collisions.
Complete the following pseudocode algorithm to write a new record to the random access file.
PROCEDURE
StoreRecord(NewData : … )
CustomerHash(NewData.CustomerID)
HashValue
"CustomerRecords.dat"
Filename
OPENFILE Filename FOR …
SEEK Filename, …
PUTRECORD Filename, …
… Filename
[5]
ENDPROCEDURE
(c) Identify two typical features of a debugger and describe how Kobi could use each one during
the development of the application.
Feature 1 …
…
…
…
Feature 2 …
…
…
…
[4]
(d) Give one benefit and one drawback of Kobi using a program generator whilst deve

12
9608/41/O/N/21 · Question 8Oct/Nov 2021Paper 4162 marks
Loading question…
Question text

8
Data entered into a computer is stored in an ordered binary tree.
The binary tree is stored in a 2D array, BinaryTree.
The first element of the array is index 0.
(a) The current contents of the binary tree are:
50
35
67
2
43
52
77
Complete the LeftPointer and RightPointer values in the following table for the binary
tree shown.
A null pointer is represented by –1.
RootNode
Index
LeftPointer
Data
RightPointer
0
[0]
50
[1]
67
[2]
77
[3]
35
[4]
2
[5]
43
[6]
52
[7]
[8]
[9]
[10]
[2]
(b) A post-order tree traversal outputs the left node, then the right node, then the root node.
In the tree given in part (a), the post-order tree traversal would output:
2
43
35
52
77
67
50
Complete the following recursive pseudocode algorithm PostOrder().
PROCEDURE PostOrder( … : INTEGER)
IF BinaryTree[RootNode, 0] <> -1
THEN
… (BinaryTree[RootNode, … ])
ENDIF
IF BinaryTree[RootNode, 2] <> -1
THEN
… (BinaryTree[RootNode, 2])
ENDIF
OUTPUT BinaryTree[RootNode, … ]
ENDPROCEDURE
[5]

13
9608/41/O/N/21 · Question 9Oct/Nov 2021Paper 4110 marks
Loading question…
Question text

9
A program uses a hashing algorithm to store data in the global array, StoredData.
The first element of the array is index 0. The array has 10 000 integer elements.
(a) Write a pseudocode declaration for the array StoredData and initialise each element to –1.
…
…
…
… [3]
(b) The hashing algorithm calculates the remainder after dividing the data by 1000, and then
adds 6 to it.
The function AddItem() takes the data as a parameter. It calculates the index to store the
data using the hashing algorithm.
If there is a collision, the function:
•
checks the next index until it finds an index that does not have data in it
•
continues to search from the start of the array, if it reaches the end of the array.
The function returns TRUE if the item was successfully added, and FALSE if the array is full.
Write program code for the function AddItem().
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

Advertisement
14
9608/42/O/N/21 · Question 1Oct/Nov 2021Paper 429 marks
Loading question…
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]

15
9608/42/O/N/21 · Question 4Oct/Nov 2021Paper 4242 marks
Loading question…
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

Advertisement