Data types, arrays and files – A Level Computer Science (old syllabus, to 2021) (9608)
118 questions · page 2 of 8
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
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().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
5
A hashtag is used on a social media network to make it easier to find messages with a specific
theme or content. A hashtag is a string consisting of a hash character ‘#’ followed by a number of
alphanumeric characters.
A message may contain several hashtag strings. A hashtag may be terminated by a space
character, the start of the next hashtag, or by the end of the message.
For example, the following message contains three hashtags:
"#Alarm34 is the result of #BatteryFailure in the #PowerModule"
The hashtags in this message are "#Alarm34", "#BatteryFailure" and "#PowerModule".
A program is being developed to monitor their use.
The program will include two global arrays each containing 10 000 elements:
•
A 1D array, TagString, of type STRING storing each hashtag in a single element of the
array. All unused array elements contain an empty string ("").
•
A 1D array, TagCount, of type INTEGER storing a count of the number of times each hashtag
is used. The count value in a given element relates to the hashtag value stored in the element
in the TagString array with the corresponding index value.
A developer has started to define the modules. Module GetStart() has already been written.
Module
Description
•
Called with two parameters:
•
a message of type 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, or value −1 if that hashtag
does not exist
•
Called with a hashtag of type STRING
•
Copies the hashtag to the next free element of the
TagString array, and sets the corresponding element of
AddHashtag()
the TagCount array to 1
•
Returns FALSE if there are no unused elements in the
TagString array, otherwise returns TRUE
•
Called with a message of type STRING
•
Searches the message for hashtags using GetStart()
CountHashtag()
•
Returns a value representing the number of hashtags in the
message
•
Called with a hashtag of type STRING
•
Increments the value of the appropriate element in the
IncrementHashtag()
TagCount array if the hashtag is found
•
Returns TRUE if the hashtag is found, or FALSE if the
hashtag is not found
(a) Write pseudocode for the module AddHashtag().
…
…
…
…
…
…
…
…
…
…
…
…
Question text
6
Consider the following diagram that represents a linked list:
Data
Pointer
A
B
C
D
Ø
The symbol Ø represents a null pointer.
(a) A new node with the data value E is added between the nodes that have the data values B
and C.
Show the state of the linked list after the node with the data value E is added.
[2]
(b) State why the node with the data value D has a null pointer.
…
… [1]
(c) A 1D array, LinkedList, is used to implement the linked list. The array is declared as a
record data type with two fields, Data and Pointer.
The global variable StartPointer stores the index of the first node in the list.
(i)
The following pseudocode algorithm finds a value in a linked list. The algorithm is
incomplete.
The function FindValue(), takes as a parameter, the value to be searched for in the
linked list. The function follows the pointers in the linked list. It either returns -1 if the
value is not found, or it returns the pointer to the value if it is found.
Complete the algorithm.
FUNCTION FindValue(Value : INTEGER) …
DECLARE CurrentPointer : INTEGER
StartPointer
CurrentPointer
WHILE … <> NULL
AND LinkedList[CurrentPointer]. … <> …
LinkedList[ … ].Pointer
CurrentPointer
ENDWHILE
IF LinkedList[CurrentPointer].Data = Value
THEN
RETURN …
ELSE
RETURN -1
ENDIF
ENDFUNCTION
[6]
(ii)
The function DeleteNode() takes the data to be removed from LinkedList as a
parameter.
The function starts at the first node and follows the pointers to check the data in each
node.
If the data is found, it removes the node containing that data, updates the pointer and
returns TRUE. Otherwise, it returns FALSE.
Write pseudocode for DeleteNode().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
7
The following table shows part of the instruction set for a processor that has one general purpose
register, the Accumulator (ACC), and an Index Register (IX).
Instruction
Explanation
Op code
Operand
LDM
#n
Immediate addressing. Load the number n to ACC.
Direct addressing. Load the contents of the location at the given
LDD
<address>
address to ACC.
Indirect addressing. The address to be used is at the given address.
LDI
<address>
Load the contents of this second address to ACC.
Indexed addressing. Form the address from <address> + the contents
LDX
<address>
of the Index Register. Copy the contents of this calculated address to
ACC.
LDR
#n
Immediate addressing. Load the number n to IX.
STO
<address>
Store the contents of ACC at the given address.
Indexed addressing. Form the address from <address> + the contents
STX
<address>
of the Index Register. Copy the contents of ACC to this calculated
address.
ADD
<address>
Add the contents of the given address to ACC.
INC
<register>
Add 1 to the contents of the register (ACC or IX).
DEC
<register>
Subtract 1 from the contents of the register (ACC or IX).
JMP
<address>
Jump to the given address.
CMP
<address>
Compare the contents of ACC with the contents of <address>.
CMP
#n
Compare the contents of ACC with number n.
Following a compare instruction, jump to <address> if the compare
JPE
<address>
was True.
Following a compare instruction, jump to <address> if the compare
JPN
<address>
was False.
IN
Key in a character and store its ASCII value in ACC.
OUT
Output to the screen the character whose ASCII value is stored in ACC.
END
Return control to the operating system.
The assembly language program in the table on the opposite page allows a username to be input
as a string of up to 8 characters in length.
For strings of less than 8 characters, the user enters the exclamation mark (!) character to
indicate that no more characters will be entered. The exclamation mark is not saved as part of the
username.
The program then outputs each character of the username in the order input.
The program will use consecutive memory locations, starting at the address labelled USERNAME,
storing one character in each location.
The program in the table is incomplete. The comment column contains descriptions for some
program instructions.
Complete the program using the given instruction set.
Instruction
Label
Comment
Op code
Operand
LDR
#0
LDM
#0
// initialise LENGTH to 0
STO
LENGTH
LOOP:
IN
// is character = EXCLAMATION(!)?
// if TRUE, jump to OUTPUT
// store character in USERNAME +
contents of IX
INC
IX
// increment Index Register
// increment LENGTH
// is LENGTH = MAX ?
// if FALSE, jump to LOOP
OUTPUT:
LDR
#0
// initialise COUNT to 0
LDX
USERNAME
OUT
INC
IX
LDD
COUNT
INC
ACC
// increment COUNT
STO
COUNT
// is COUNT = LENGTH ?
JPN
OUTPUT
END
// end program
LENGTH:
EXCLAMATION:
B0010001
MAX:
8
COUNT:
USERNAME:
[8]
Question text
1
There are several different searching and sorting algorithms.
(a) Identify two sorting algorithms.
1 …
2 …
[2]
(b) Consider the following pseudocode algorithm.
0
LowerBound
LengthOfList – 1
UpperBound
FALSE
ValueFound
OUTPUT "Value to find: "
INPUT ValueToFind
WHILE ValueFound = FALSE AND UpperBound <> LowerBound
(LowerBound + UpperBound)DIV 2
MidPoint
IF List[MidPoint] = ValueToFind
THEN
TRUE
ValueFound
ELSE
IF List[MidPoint] < ValueToFind
THEN
MidPoint + 1
LowerBound
ELSE
MidPoint – 1
UpperBound
ENDIF
ENDIF
ENDWHILE
IF ValueFound = FALSE
THEN
(LowerBound + UpperBound) DIV 2
MidPoint
IF List[MidPoint] = ValueToFind
THEN
OUTPUT "Item in position " & MidPoint & " in list"
ELSE
OUTPUT "Not in list"
ENDIF
ELSE
OUTPUT "Item in position " & MidPoint & " in list"
ENDIF
Note: DIV is an operator that performs integer division.
The array List contains the following values:
2, 5, 21, 25, 36, 48, 51, 59, 65, 70
(i)
Complete the trace table to show a dry run of the algorithm, when the value 21 is input.
LowerBound
UpperBound
ValueFound
ValueToFind
MidPoint
[3]
(ii)
Identify this type of searching algorithm.
… [1]
(iii)
The value 59 is input.
State the number of times the while loop condition is executed.
… [1]
(iv)
State the minimum number of times the while loop condition will be executed to search
for a value.
… [1]
(v)
MidPoint is calculated and checked again after the while loop is terminated.
Explain why this additional calculation and check is necessary.
…
…
…
… [2]
(vi)
A new data set is used as follows:
5, 9, 10, 12, 15, 13, 17, 19, 20, 2
Explain why the algorithm will not find the value 2 in this data set.
…
…
…
… [2]
Question text
5
The following table shows part of the instruction set for a processor that has one general purpose
register, the Accumulator (ACC), and an Index Register (IX).
Instruction
Explanation
Op code
Operand
LDM
#n
Immediate addressing. Load the number n to ACC.
Direct addressing. Load the contents of the location at the given
LDD
<address>
address to ACC.
Indirect addressing. The address to be used is at the given address.
LDI
<address>
Load the contents of this second address to ACC.
Indexed addressing. Form the address from <address> + the contents
LDX
<address>
of the Index Register. Copy the contents of this calculated address to
ACC.
LDR
#n
Immediate addressing. Load the number n to IX.
STO
<address>
Store the contents of ACC at the given address.
Indexed addressing. Form the address from <address> + the contents
STX
<address>
of the Index Register. Copy the contents of ACC to this calculated
address.
ADD
<address>
Add the contents of the given address to ACC.
INC
<register>
Add 1 to the contents of the register (ACC or IX).
DEC
<register>
Subtract 1 from the contents of the register (ACC or IX).
JMP
<address>
Jump to the given address.
CMP
<address>
Compare the contents of ACC with the contents of <address>.
CMP
#n
Compare the contents of ACC with number n.
Following a compare instruction, jump to <address> if the compare
JPE
<address>
was True.
Following a compare instruction, jump to <address> if the compare
JPN
<address>
was False.
IN
Key in a character and store its ASCII value in ACC.
OUT
Output to the screen the character whose ASCII value is stored in ACC.
END
Return control to the operating system.
Consider the following pseudocode algorithm:
0
Length
INPUT Character
WHILE Character <> "."
Message
Message & Character
Length
Length + 1
INPUT Character
ENDWHILE
Complete the table by writing assembly language code for the algorithm, using the given instruction
set.
Instruction
Label
Comment
Op code
Operand
// initialise IX to zero
// initialise LENGTH
LOOP:
// input character
// is character a FULLSTOP (.) ?
// jump to ENDP if TRUE
// store character in MESSAGE +
contents of IX
// increment IX
// increment LENGTH
// jump to LOOP
ENDP:
END
// end program
LENGTH:
FULLSTOP:
B01100000
// ASCII code for a full stop (.)
MESSAGE:
[8]
Question text
6
The following diagram represents a linked list.
Data
Pointer
A
B
C
D
Ø
The symbol Ø represents a null pointer.
(a) The node with the data value C is removed from the list.
Show the new state of the linked list.
[2]
(b) State what happens to the node with the data value C when it is removed from the list.
…
… [1]
(c) State what is meant by a null pointer.
… [1]
(d) The linked list is implemented as a 1D array, LinkedList. The array is declared as a record
data type, with two fields, Data and Pointer.
The function FindValue() takes as a parameter, the value to be searched for in the linked
list. The function follows the pointers in the linked list. It returns -1 if the value is not found, or
it returns the pointer to the value if it is found.
The global variable StartPointer points to the first element in the list.
Write pseudocode for the function FindValue().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [8]
9608/42/O/N/
Question text
5
(a) A 1D array, Directory, of type STRING is used to store a list of school internal telephone
numbers. There are 1000 elements in the array. Each element stores a single data item. The
format of each data item is as follows:
<Number><Name>
Number is a four-digit numeric string.
Name is a variable-length string.
For example:
"1024Collins Robbie"
The following pseudocode is an initial attempt at defining a procedure SortContacts()
that will perform a bubble sort on Directory. The array is to be sorted in ascending order of
Name.
Fill in the gaps to complete the pseudocode.
Refer to the Appendix on page 21 for a list of built-in pseudocode functions and operators.
PROCEDURE SortContacts ()
DECLARE Temp : STRING
DECLARE FirstName, SecondName : STRING
DECLARE NoSwaps : …
DECLARE Boundary, J : INTEGER
…
Boundary
REPEAT
TRUE
NoSwaps
1 TO Boundary
FOR J
… (Directory[J], LENGTH(Directory[J]) – … )
FirstName
RIGHT(Directory[J + 1], LENGTH(Directory[J + 1]) – 4)
SecondName
IF FirstName …
THEN
Directory[J]
Temp
Directory …
Directory[J]
Temp
Directory[J + 1]
…
NoSwaps
ENDIF
ENDFOR
…
Boundary
UNTIL NoSwaps = TRUE
ENDPROCEDURE
[8]
(b) The pseudocode contains a mechanism designed to make this an efficient bubble sort.
Describe the mechanism and explain why it may be considered to be efficient.
…
…
…
…
…
…
…
…
…
… [4]