Data types, arrays and files – A Level Computer Science (old syllabus, to 2021) (9608)
Quick quiz: 10 random questions from this topic. Answer them all, then open the Summary.
Question text
6
An Abstract Data Type (ADT) is used to create an unordered binary tree. The binary tree is created
as an array of nodes. Each node consists of a data value and two pointers.
A record type, Node, is declared using pseudocode.
TYPE Node
DECLARE DataValue : STRING
DECLARE LeftPointer : INTEGER
DECLARE RightPointer : INTEGER
ENDTYPE
The following statement declares an array BinaryTree.
DECLARE BinaryTree : ARRAY[0:14] OF Node
A variable, NextNode, points to the next free node.
The following diagram shows a possible node.
ArrayIndex
DataValue
LeftPointer
RightPointer
0
"Red"
1
2
The commands in the following table create and add nodes to the binary tree.
Command
Comment
CreateTree(NodeData)
Sets NextNode to 0.
Writes NodeData into DataValue at the position NextNode
Updates NextNode using NextNode = NextNode + 1
AttachLeft(NodeData, ParentNode)
Writes NodeData into DataValue of NextNode
Sets the LeftPointer of node ParentNode to NextNode
Updates NextNode using NextNode = NextNode + 1
AttachRight(NodeData, ParentNode)
Writes NodeData into DataValue of NextNode
Sets the RightPointer of node ParentNode to NextNode
Updates NextNode using NextNode = NextNode + 1
(a) The following commands are executed.
CreateTree("Red")
AttachLeft("Blue", 0)
AttachRight("Green", 0)
The following diagram shows the current state of the binary tree.
0
"Red"
1
2
1
"Blue"
2 "Green"
…
…
…
…
…
…
…
…
…
…
…
…
Write on the diagram to show the state of the binary tree after the following commands have
been executed.
AttachRight("Black", 2)
AttachLeft("Brown", 2)
AttachLeft("Peach", 3)
AttachLeft("Yellow", 1)
AttachRight("Purple", 1)
AttachLeft("White", 6)
AttachLeft("Pink", 7)
AttachLeft("Grey", 9)
AttachRight("Orange", 9)
[5]
(b) A new command has been added to initialise the pointers of the binary tree to − 1 to indicate
they are not in use.
A leaf is a node of the binary tree which has no children. In the case of this binary tree, a node
with a LeftPointer of − 1 and a RightPointer of − 1 is a leaf.
Write a recursive function, in program code, to traverse the binary tree and output the value
of DataValue for each leaf node.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…
Question text
4
The following pseudocode is a string handling function.
For the built-in functions list, refer to the Appendix on page 16.
FUNCTION Clean(InString : STRING) RETURNS STRING
DECLARE NewString : STRING
DECLARE Index : INTEGER
DECLARE AfterSpace : BOOLEAN
DECLARE NextChar : CHAR
CONSTANT Space = ' '
FALSE
AfterSpace
""
NewString
1 TO LENGTH(InString)
FOR Index
MID(InString, Index, 1)
NextChar
IF AfterSpace = TRUE
THEN
IF NextChar <> Space
THEN
NewString & NextChar
NewString
FALSE
AfterSpace
ENDIF
ELSE
NewString & NextChar
NewString
IF NextChar = Space
THEN
TRUE
AfterSpace
ENDIF
ENDIF
ENDFOR
RETURN NewString
ENDFUNCTION
(a) (i)
Complete the trace table by performing a dry run of the function when it is called as
follows:
Clean("X∇∇∇Y∇and∇∇Z")
Result
The symbol '∇' represents a space character. Use this symbol to represent a space
character in the trace table.
Index
AfterSpace
NextChar
NewString
[6]
(ii)
State the effect of the function Clean().
…
… [1]
(iii)
The pseudocode is changed so that the variable AfterSpace is initialised to TRUE.
Explain what will happen if the function is called as follows:
Clean("∇∇X∇∇∇Y∇and∇∇Z")
Result
…
…
…
… [2]
(b) The following pseudocode declares and initialises an array.
DECLARE Code : ARRAY[1:100] OF STRING
DECLARE Index : INTEGER
1 TO 100
FOR Index
""
Code[Index]
ENDFOR
The design of the program is changed as follows:
•
the array needs to be two dimensional, with 500 rows and 4 columns
•
the elements of the array need to be initialised to the string "Empty"
Re-write the pseudocode to implement the new design.
…
…
…
…
…
…
…
…
…
… [4]
(c) State the term used for changes that are made to a program in response to a specification
ch
Question text
3
Recursive algorithms can be used when creating programs.
(a) Describe what is meant by a recursive algorithm.
…
…
…
…
…
… [3]
(b) A string that is a palindrome, reads the same forwards as it does backwards. For example,
the name Anna is a palindrome.
The function Substring(Variable, StartingCharacter, NumberOfCharacters)
returns one or more characters from a string. The first character is at position 0.
For example, the string "Happy" is stored in the variable Word.
•
Substring(Word, 1, 1) would return the character "a".
•
Substring(Word, 2, 3) would return the characters "ppy".
The function Length() returns the length of the string as an integer. For example,
Length(Word) returns 5.
The following is a recursive function to find out whether a string is a palindrome. The function
returns True if the parameter is a palindrome, and returns False if it is not a palindrome.
Complete the pseudocode for the recursive algorithm to indicate whether a string is a
palindrome.
FUNCTION IsPalindrome(CheckWord : STRING) RETURNS BOOLEAN
IF … <= 1
THEN
RETURN …
ENDIF
IF Substring(CheckWord, 0, 1) <>
Substring(CheckWord, … (CheckWord)-1, 1)
THEN
RETURN …
ELSE
RETURN … ( Substring(CheckWord, 1,
Length(CheckWord)-2))
ENDIF
ENDFUNCTION
[5]
(c) The function FindPower() is a recursive function that calculates the result of a base number
to the power of the exponent. For example, the result of 2 4 = 16, as 2*2*2*2 = 16. In this
example, 2 is the base number and 4 is the exponent.
The base number and the exponent are passed as parameters.
Write pseudocode for the recursive function FindPower(). Assume both the base and the
exponent are positive integers.
…
…
…
…
…
…
…
…
…
Question text
1
Each student at CIE University needs a printing account to print documents from university
computers.
The university is developing software to manage each student’s printing account and the printing
process.
(a) Developing the software will include the following activities.
Time in
Activity
Description
Predecessor
weeks
A
Identify requirements
1
-
B
Produce design
3
A
C
Write code
10
B
D
Test modules
7
B
E
Final system black-box testing
3
C, D
F
Install software
1
E
G
Acceptance testing
2
F
H
Create user documentation
2
F
(i)
Add the correct activities and times to the following Program Evaluation Review
Technique (PERT) chart for the software development.
Two activities and times have been done for you.
4
A
1
2
3
5
6
1
1
F
7
8
9
[6]
(ii)
State what is meant by the critical path in a PERT chart.
…
… [1]
(iii)
Identify and describe a project planning technique, other than a PERT chart.
…
…
…
… [2]
(b) When a student prints a document, a print job is created. The print job is sent to a print server.
The print server uses a queue to hold each print job waiting to be printed.
(i)
The queue is circular and has six spaces to hold jobs.
The queue currently holds four jobs waiting to be printed. The jobs have arrived in the
order A, B, D, C.
Complete the diagram to show the current contents of the queue.
Start Pointer
End Pointer
[1]
(ii)
Print jobs A and B are now complete. Four more print jobs have arrived in the
order E, F, G, H.
Complete the diagram to show the current contents and pointers for the queue.
[3]
(iii)
State what would happen if another print job is added to the queue in the status
in part (b)(ii).
…
… [1]
(iv)
The queue is stored as an array, Queue, with six elements. The following algorithm
removes a print job from the queue and returns it.
Complete the following pseudocode for the function Remove.
FUNCTION Remove RETURNS STRING
DECLARE PrintJob : STRING
IF … = EndPointer
THEN
RETURN "Empty"
ELSE
PrintJob
Queue[ … ]
IF StartPointer = …
THEN
…
StartPointer
ELSE
StartPointer
StartPointer + 1
ENDIF
RETURN PrintJob
ENDIF
ENDFUNCTION
[4]
(v)
Explain why the circular queue could not be implemented as a stack.
…
…
…
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
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
Question text
(a) A program stores data about hospital patients.
1
Give a suitable identifier name for each of the data items.
Description of data item
Suitable identifier name
The temperature of the patient
The temperature of the room
The patient identification number
The name of the nurse taking the measurement
[4]
Program variables have values as follows:
(b) (i)
Variable
Value
MyGreeting
"Happy Birthday"
MyInitial
'C'
AgeInYears
27
Weight
60.5
Married
TRUE
Children
TRUE
Evaluate each expression in the following table.
If an expression is invalid, write ERROR.
For the built-in functions list, refer to the Appendix on page 15.
Expression
Evaluates to
"Mon" & MID(MyGreeting, 10, 2)
AgeInYears + ASC(MyInitial)
INT(MyInitial)
MOD(Weight * 2, 10)
Married AND (NOT Children)
[5]
Programming languages support different data types.
(ii)
Give an appropriate data type for each of these variables from part (b)(i).
Variable
Data type
MyGreeting
MyInitial
AgeInYears
Weight
Married
[5]
Question text
3
The following structure chart shows the relationship between three modules.
TopLevel
B
D
A
C
E
SubA
SubB
Parameters A to E have the following data types:
A, D : STRING
C
: CHAR
B, E : INTEGER
(a) (i)
Write the pseudocode header for module SubA().
…
…
… [3]
(ii)
Write the pseudocode header for module SubB().
…
…
… [3]
(b) Module hierarchy and parameters are two features that may be represented on a structure
chart.
State two other features than can be represented.
Feature 1 …
Feature 2 …
[2]
Question 4 begins on the next page.
Question text
3
A bubble sort algorithm is used to sort an integer array, List. This algorithm can process arrays
of different lengths.
(a) Write pseudocode to complete the bubble sort algorithm shown.
… TO 0 STEP - 1
01 FOR Outer
0 TO ( … )
02
FOR Inner
IF …
…
03
>
04
THEN
…
05
Temp
…
06
List[Inner]
…
07
List[Inner + 1]
08
ENDIF
09
ENDFOR
10 ENDFOR
[7]
(b) (i)
State the order of the sorted array.
… [1]
(ii)
State which line of the algorithm you would change to sort the array into the opposite
order.
State the change you would make.
Line …
Change …
…
[1]
(c) Use pseudocode to write an alternative version of this bubble sort algorithm that will exit the
algorithm when the list is fully sorted.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
4
Numeric formatting converts a numeric value to a string in order to present it in a specific way.
In a generic high-level language, formatting is implemented using a mask system. In this system,
each character of the mask corresponds to one character of the formatted string.
Mask characters have the following meaning:
Mask character
Meaning
Character must be a digit or a space
#
Character must be a digit
0
Any other mask characters are taken as literal values and are included in the formatted string.
(a) Using the mask "###00.00", complete the following table. Use □ to represent a space. The
first value has been done for you.
Value
Formatted string
1327.5
"□1327.50"
1234
7.456
[2]
(b) For each row in the following table, define the mask required to produce the formatted output
from the given value. □ represents a space.
Value
Required output
Mask
1234.00
"1,234.00"
3445.66
"£3,445.66"
10345.56
"$□ □10,345"
[3]