Data types, arrays and files – A Level Computer Science (old syllabus, to 2021) (9608)
58 questions · page 1 of 4 · Clear filters
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]
Question text
6
A company hires out rowing boats on a lake. The company has ten boats, numbered from 1 to 10.
The company is developing a program to help manage and record the hiring out process.
Hire information is stored in three global 1D arrays when a boat is hired out. Each array contains
10 elements representing each of the ten boats.
The three 1D arrays are summarised as follows:
Array
Data type
Description
Example data value
HireTime
STRING
"10:15"
The time the boat was hired out
Duration
INTEGER
30
The number of minutes of the hire
Cost
REAL
5.75
The cost of the hire
If an individual boat is not currently on hire, the corresponding element of the HireTime array will
be set to "Available".
The programmer has started to define program modules as follows:
Module
Description
•
Called with two parameters:
o a STRING value representing a time
AddTime()
o an INTEGER value representing a duration in minutes
•
Adds the duration to the time to give a new time
•
Returns the new time as a STRING
•
Called with a STRING value representing the time the
hire will start
•
Outputs the boat numbers that will be available for hire at
the given start time. A boat will be available for hire if it is
either:
ListAvailable()
o currently not on hire, or
o due back before the given hire start time
•
Outputs the number of each boat available
•
Outputs the total number of boats available or a suitable
message if there are none
•
Called with four parameters:
o an INTEGER value representing the boat number
o a STRING value representing the hire start time
o an INTEGER value representing the hire duration in
minutes
o a REAL value representing the cost of hire
RecordHire()
•
Updates the appropriate element in each array
•
Adds the cost of hire to the global variable DailyTakings
•
Converts the four input parameters to strings,
concatenated using commas as separators, and writes
the resulting string to the end of the existing text file
HireLog.txt
(a) Write pseudocode for the module ListAvailable().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
4
(a) Mustafa has developed the following pseudocode to generate ten random integers in the
range 1 to 100.
DECLARE Random : ARRAY [1:10] OF INTEGER
DECLARE Count, RNum : INTEGER
1 TO 10
FOR Count
INT(RAND(100)) + 1
Rnum
RNum
Random[Count]
ENDFOR
Refer to the Appendix on page 19 for a list of built-in pseudocode functions and operators.
Rewrite the pseudocode so that there are no duplicated numbers in the list of random
numbers.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
(b) The changes made to the pseudocode in part (a) were as a result of changes to the program
requirement.
Give the term used to describe changes made for this reason.
…
Question text
5
A global 1D array, Contact, of type STRING is used to store a list of names and email addresses.
There are 1000 elements in the array. Each element stores one data item. The format of each
data item is as follows:
<Name>':'<EmailAddress>
Name and EmailAddress are both variable-length strings.
For example:
"Sharma Himal:[email protected]"
A function, GetName(), is part of the program that processes the array. A data item string will be
passed to the function as a parameter. The function will return the Name part. Validation is not
necessary.
(a) Use structured English to describe the algorithm for the function GetName().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [7]
(b) (i)
The array is to be sorted using an efficient bubble sort algorithm. An efficient bubble sort
reduces the number of unnecessary comparisons between elements.
Describe how this could be achieved.
…
…
…
…
…
Question text
2
(a) A structure chart is often produced as part of a modular program design. The chart shows the
relationship between modules and the parameters that are passed between them.
Give two other features the structure chart can show.
Feature 1 …
…
Feature 2 …
…
[2]
(b) The following structure chart shows the relationship between three modules.
ModuleA()
ParW
ParX
ParZ
ModuleB()
ModuleC()
Parameter data types are:
ParW : REAL
ParX : INTEGER
ParZ : STRING
(i)
Write the pseudocode header for module ModuleB().
…
…
… [3]
(ii)
Write the pseudocode header for module ModuleC().
…
…
… [3]
(c) A student is developing an algorithm to count the number of times a given string,
SearchString, appears in a 2D array. The array, Item, consists of 100 elements organised
as 50 rows of 2 columns. SearchString could appear in any row or column.
The array is declared in pseudocode as follows:
DECLARE Item : ARRAY [1:50, 1:2] OF STRING
The structured English description of the algorithm is:
1.
SET Count to 0.
2.
Examine the first row of the array.
3.
IF column 1 element value is equal to SearchString, ADD 1 to Count.
4.
IF column 2 element value is equal to SearchString, ADD 1 to Count.
5.
REPEAT from step 3 for next row, UNTIL row is last row.
6.
OUTPUT a suitable message and Count.
Write pseudocode for the algorithm.
…
…
…
…
…
…
…
…
…
Question text
4
A global 1D array, Contact, of type STRING is used to store a list of names and email addresses.
There are 1000 elements in the array. Each element stores one data item. The format of each
data item is as follows:
<Name>':'<EmailAddress>
Name and EmailAddress are both variable-length strings.
For example:
"Wan Zhu:[email protected]"
A function, Extract(), is part of the program that processes the array. A string data item is
passed to the function as a parameter. The function will return the Name part. Validation is not
necessary.
(a) Write program code for the function Extract().
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]
(b) The original function, Extract(), needs to be modified to separate the name from the email
address. The calling program can then use both of these values.
Write, in pseudocode, the header for the modified subroutine. Explain the changes you have
made.
Subroutine header …
Question text
6
A theatre company stores customer login details to allow customers to book tickets online.
A hash table stores login details for 2000 customers.
Each customer’s details are stored in a record.
The declaration for CustomerRecord is:
TYPE CustomerRecord
DECLARE UserID : STRING
DECLARE PINNumber : INTEGER
ENDTYPE
A 1D array, CustomerDetails, is used to implement the hash table. CustomerDetails is a
global array. The 1D array has 6000 elements.
(a) The procedure InitialiseHashTable() initialises the hash table. UserID is initialised as
an empty string, and PINNumber initialised to 0 for all of the records.
Write pseudocode for the procedure InitialiseHashTable().
…
…
…
…
…
…
…
… [4]
(b) The function InsertRecord() is used to insert a new record into the hash table.
The function, Hash():
•
takes a UserID as a parameter
•
performs the hashing algorithm
•
returns the calculated index of the user ID within the hash table.
If the hash table is full, the function InsertRecord() returns -1. If there is space available
in the hash table, the record is inserted, and it returns the position of this record in the array.
Complete the pseudocode for the function.
FUNCTION InsertRecord(NewRecord : CustomerRecord) RETURNS INTEGER
DECLARE Count : INTEGER
DECLARE Index : INTEGER
0
Count
Hash( … )
Index
WHILE (CustomerDetails[Index].UserID <> "") … (Count <= 5999)
Index + 1
Index
Count + 1
Count
IF Index > 5999
THEN
…
ENDIF
ENDWHILE
IF Count > 5999
THEN
…
ELSE
…
CustomerDetails[ … ]
…
ENDIF
ENDFUNCTION
[7]
Question text
7
(a) A shirt design company has an order form to order shirts. Customers can order multiple shirts
using the same form.
The customer details section has the data:
•
name
•
address
•
telephone number.
The order details section has the data:
•
shirt ID
•
colour
•
cost.
A total cost for the order is also calculated.
The cost of each shirt is dependent on the size ordered. The sizes customers can order are
small, medium and large.
Complete the following JSP data structure diagram for the order form.
Order Form
Customer Details
Total Cost
[7]
(b) Each customer’s order is stored as a record in a file. The customers’ orders are stored in the
order in which they arrive in the file and no key field is used.
(i)
Identify this type of file structure.
… [1]
(ii)
Identify two other types of file structure.
1 …
2 …
[2]
(c) The procedure UpdateTelephone() allows the shirt company to update the record for a
customer’s details. The procedure will update the telephone number.
The program stores customer details as a custom data type, Customer.
The definition for this data type is:
TYPE Customer
Name : STRING
Address : STRING
TelephoneNumber : STRING
ENDTYPE
The procedure UpdateTelephone() takes the customer record to be updated and the new
telephone number as parameters. It then updates the telephone number in the record.
Complete the pseudocode for the procedure UpdateTelephone().
PROCEDURE UpdateTelephone( … ThisCustomer : Customer,
… NewTelephoneNumber : STRING)
…
ENDPROCEDURE
[3]
(d) The shirt company is looking to implement a system to reward customers. The system
includes:
•
10% discount on orders over $50
•
free gift if order over $50 and if the order is placed on a Monday
•
additional 5% discount if a customer has a loyalty card
•
free delivery for a customer with a loyalty card and spends over $50.
Complete the following decision table for this system.
Order over $50
Y
Y
Y
Y
N
N
N
N
Conditions
Monday
Y
Y
N
N
Y
Y
N
N
Loyalty card
Y
N
Y
N
Y
N
Y
N
Additional 5% discount
10% discount
Actions
Free gift
Free delivery
[4]
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
Question text
2
A business is developing a program that stores each customer’s username and password in a
hash table.
The hash table will be implemented as a 1D array, CustomerLogIn, of the custom data type
CustomerRecord.
The declaration for CustomerRecord is:
TYPE CustomerRecord
DECLARE Username : STRING
DECLARE Password : STRING
ENDTYPE
The hash table will store a maximum of 3000 records. The key field will be Username.
(a) The program declares the hash table and initialises the username and password of all the
records to an empty string.
Write pseudocode to declare and initialise the hash table.
…
…
…
…
…
…
…
… [4]
(b) (i)
A function, SearchHashTable() will search for a customer’s record in the hash table.
The function Hash():
•
takes a Username as a parameter
•
performs the hashing algorithm
•
returns the calculated index of the username within the hash table.
The function SearchHashTable():
•
takes the username to search for as a parameter
•
uses Hash() to calculate the first index of this username within the hash table
•
returns either the index of the username if found, or –1 if not found.
Complete the pseudocode for the function SearchHashTable().
FUNCTION SearchHashTable(BYVALUE SearchUser : STRING) RETURNS …
DECLARE Index : INTEGER
DECLARE Count : INTEGER
… (SearchUser)
Index
Count
0
WHILE (CustomerLogIn[Index] … <> … )
AND(CustomerLogIn[Index].Username <> "")
AND(Count < 2999)
Index
Index + 1
Count
Count + 1
IF Index > …
THEN
Index
0
ENDIF
ENDWHILE
IF CustomerLogIn[Index].Username = …
THEN
RETURN Index
ELSE
RETURN …
ENDIF
ENDFUNCTION
[7]
(ii)
Explain the purpose of the variable Count in the function SearchHashTable().
…
…
…
… [2]
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
8
Files can be structured in serial, sequential or random format.
Tick (3) one box in each row to show whether the statement applies to Serial, Sequential or
Random format.
Statement
Serial
Sequential
Random
Uses a hashing algorithm
No key field is used when storing data, for example,
it is stored in chronological order
Collisions can occur
Least efficient for a very large number of records
Most efficient for a very large number of records
[3]
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
Question text
1
(a) (i)
Algorithms may be expressed using four basic constructs. One construct is sequence.
Complete the following table for two other constructs.
Construct
Pseudocode example
…
…
…
…
…
…
…
…
…
…
[4]
(ii)
Simple algorithms usually consist of input, process and output.
Complete the table by placing ticks (‘3’) in the relevant boxes.
Pseudocode statement
Input
Process
Output
SensorValue * Factor
Temp
WRITEFILE "LogFile.txt", TextLine
WRITEFILE "LogFile.txt", MyName & MyIDNumber
READFILE "AddressBook.txt", NextLine
[4]
(b) Program variables have values as follows:
Variable
Value
Title
"101 tricks with spaghetti"
Version
'C'
Author
"Eric Peapod"
PackSize
4
WeightEach
6.2
Paperback
TRUE
(i)
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 16.
Expression
Evaluates to
MID(Title, 5, 3) & RIGHT(Author, 3)
INT(WeightEach * PackSize)
PackSize >= 4 AND WeightEach < 6.2
LEFT(Author, ASC(Version) - 65)
RIGHT(Title, (LENGTH(Author) – 6))
[5]
(ii)
Programming languages support different data types.
Give an appropriate data type for the following variables from part (b).
Variable
Data type
Title
Version
PackSize
WeightEach
Paperback
[5]
(c) White-box and black-box are two types of testing. In white-box testing, data are chosen to
test every possible path through the program.
Explain how data are chosen in black-box testing.
…
… [2]
Question text
3
(a) A student is developing an algorithm to search through a 1D array of 100 elements. Each
element of the array, Result, contains a REAL value.
The algorithm will output:
•
the average value of all the elements
•
the number of elements with a value of zero.
The structured English description of the algorithm is:
1.
SET Total value to 0
2.
SET Zero count to 0
3.
SELECT the first element
4.
ADD value of element to Total value
5.
IF element value is 0 then INCREMENT Zero count
6.
REPEAT from step 4 for next element, until element is last element
7.
SET Average to Total / 100
8.
OUTPUT a suitable message and Average
9.
OUTPUT a suitable message and Zero count
Write pseudocode for this algorithm.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [7]
(b) The student decides to change the algorithm and implement it as a procedure, ScanArray(),
which will be called with three parameters.
ScanArray(AverageValue, ZeroCount, ArrayName)
ScanArray() will modify the first two parameters so that the new values are available to the
calling program or module.
Write the pseudocode procedure header for ScanArray().
…
…
…
…
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
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.