Data types, arrays and files – A Level Computer Science (old syllabus, to 2021) (9608)
70 questions · page 1 of 5 · Clear filters
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
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
…
…
…
…
…
…
…
…
…
…
…
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
…
…
…
…
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
…
…
…
…
…
…
…
…
…
…
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().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
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
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
…
…
…
…
…
…
…
…
…
…
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
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
3
The procedure OutputLines() outputs a number of lines from a text file.
An example of the use of the procedure is given by the following pseudocode:
CALL OutputLines(FileName, StartLine, NumberLines)
Parameter
Data type
Description
FileName
STRING
The name of the text file
StartLine
INTEGER
The number of the first line to be output
NumberLines
INTEGER
The number of lines to be output
The procedure is tested using the file MyFile.txt that contains 100 lines of text.
The procedure gives the expected result when called as follows:
CALL OutputLines("MyFile.txt", 1, 10)
(a) The procedure is correctly called with three parameters of the appropriate data types, but the
procedure does not give the expected result.
Give three different reasons why this might happen.
1 …
…
2 …
…
3 …
…
[3]
(b) Write program code for the procedure OutputLines().
Note: Parameter validation is not necessary.
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
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
5
A message may contain several hashtags.
A hashtag is a string consisting of a hash character ‘#’, followed by one or more alphanumeric
characters.
A hashtag may be terminated by a space character, the start of the next hashtag, any other
non-alphanumeric character, or by the end of the message.
For example, the following message contains three hashtags:
"#Error27 is the result of #PoorPlanning by the #Designer"
The hashtags in the message are "#Error27", "#PoorPlanning" and "#Designer".
A program is being developed to process a message and extract each hashtag.
A global 1D array of strings, TagString, will store each hashtag in a single element.
Unused array elements will contain an empty string. The array will contain 10 000 elements.
A developer has started to define the modules as follows:
Module
Description
•
Called with two parameters:
•
a message string
•
an integer giving the number of the required hashtag.
For example, GetStart(Message, 3) would search
GetStart()
for the third hashtag in the string Message
•
Returns an integer value representing the start position
of the hashtag in the message string, or value −1 if that
hashtag does not exist
•
Called with two parameters:
•
a message string
•
an integer giving the hashtag start position within the
GetTag()
message
•
Returns the hashtag or an empty string if the character in
the message at the hashtag start position is not '#'
•
Called with a hashtag as a parameter
•
Returns the index position of the hashtag in array
GetIndex()
TagString
•
Returns the value −1 if the hashtag is not present in the
array
(a) Write pseudocode for the module GetIndex().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
(a) Algorithms usually consist of three different stages.
One stage is INPUT.
Name the other stages.
1 …
2 …
[1]
(b) An algorithm may be documented using different methods. These include structured English,
a program flowchart, and pseudocode.
State what a program designer represents using one or more of these methods.
…
… [2]
(c) Programming languages support different data types.
Complete the table by giving four different data types together with an example data value
for each.
Data type
Example data value
[4]
(d) Draw lines to connect each of the following computing terms with the appropriate description.
Term
Description
A structure for the
Black-box testing
temporary storage of data
A method used when the
File
structure of the program is
unknown
A method of setting the
Assignment
value of a variable
A structure for the
Array
permanent storage of data
[3]
(e) A pseudocode algorithm assigns values to three variables as follows:
TRUE
FlagA
FALSE
FlagB
TRUE
FlagC
Evaluate the expressions given in the following table:
Expression
Evaluates to
NOT FlagB AND FlagC
NOT (FlagB OR FlagC)
(FlagA AND FlagB) OR FlagC
NOT (FlagA AND FlagB) OR NOT FlagC
[2]
Question text
5
A hashtag is used on a social media network. A hashtag is a string consisting of a hash character
‘#’ followed by one or more alphanumeric characters.
A program is being developed to monitor the use of hashtags.
The program will include two global arrays each containing 10 000 elements:
•
A 1D array, TagString, of type STRING stores each hashtag in a single element. All unused
array elements contain an empty string ("").
•
A 1D array, TagCount, of type INTEGER stores a count of the number of times each hashtag
is used. The count value at a given index relates to the element stored at the corresponding
index in the TagString array.
The contents of the two arrays will be stored in a text file Backup.txt. The format of each line of
the file is:
<Hashtag><','><Count>
For example:
"#ComputerScienceClass,978"
A developer has started to define the modules as follows:
Module
Description
•
InitArrays()
Initialise the arrays
•
The contents of the two arrays are stored in the text file Backup.txt
Existing file contents will be overwritten
•
Each hashtag and count are stored in one line of the file, as in the example
SaveArrays()
above
•
Unused TagString elements are not added to the file
•
Returns the total number of unused TagString elements
•
Values from the text file Backup.txt are stored in the two arrays
LoadArrays()
•
The number of elements stored is returned
(a) Write pseudocode for the module InitArrays().
…
…
…
…
…
…
…
…
…
… [4]
(b) Write pseudocode for the module SaveArrays().
…
…
…
…
…
…
…
Question text
1
(a) A programmer uses the process of stepwise refinement to break down a problem.
Explain the purpose of stepwise refinement.
…
…
…
… [2]
(b) Programming languages support different data types. These usually include STRING and
REAL.
Complete the table by giving four other data types and an example data value for each.
Data type
Example data value
[4]
(c) An experienced programmer is working on a program that is written in a language she is not
familiar with.
(i)
State one feature of the program that she should be able to recognise.
…
… [1]
(ii)
State the type of skill that would allow her to recognise this feature.
…
… [1]
(d) Give three methods that may be used to identify and locate errors in a program after it has
been written.
You may include one feature found in a typical Integrated Development Environment (IDE).
1 …
2 …
3 …
[3]
Question text
3
A global 1D array, ProdNum, of type INTEGER contains 5 000 elements and is used to store
product numbers.
A procedure is needed to sort ProdNum into ascending order using a bubble sort algorithm.
Write program code for the procedure BubbleSort().
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
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
4
(a) The following pseudocode includes a procedure that searches for a value in a 1D array and
outputs each position in the array where the value is found.
Refer to the Appendix on page 16 for the list of built-in functions and operators.
DECLARE NameList : ARRAY [1:100] OF STRING
DECLARE SearchString : STRING
PROCEDURE Search()
DECLARE Index : INTEGER
1 TO 100
FOR Index
IF NameList[Index] = SearchString
THEN
OUTPUT "Found at " & NUM_TO_STRING(Index)
ENDIF
ENDFOR
ENDPROCEDURE
The specification of module Search() changes. The pseudocode needs to be amended to
meet a new requirement.
The procedure needs to be implemented as a function, Search(), which will:
•
take the search value as a parameter
•
return an integer which is:
either the index value where the search value is first found
•
or –1 if the search value is not found.
•
Write the pseudocode for the function Search().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…