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

118 questions · page 3 of 8

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
31
9608/21/M/J/20 · Question 6May/June 2020Paper 2119 marks
Loading question…
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().
…
…
…
…
…
…
…
…
…
…
…
…
…
…

32
9608/22/M/J/20 · Question 4May/June 2020Paper 227 marks
Loading question…
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.
…

33
9608/22/M/J/20 · Question 5May/June 2020Paper 2219 marks
Loading question…
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.
…
…
…
…
…

Advertisement
34
9608/23/M/J/20 · Question 2May/June 2020Paper 2313 marks
Loading question…
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.
…
…
…
…
…
…
…
…
…

35
9608/23/M/J/20 · Question 4May/June 2020Paper 238 marks
Loading question…
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 …

36
9608/41/M/J/20 · Question 6May/June 2020Paper 4111 marks
Loading question…
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]

37
9608/41/M/J/20 · Question 7May/June 2020Paper 4117 marks
Loading question…
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

38
9608/42/M/J/20 · Question 2May/June 2020Paper 4213 marks
Loading question…
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]

Advertisement
39
9608/42/M/J/20 · Question 3May/June 2020Paper 4213 marks
Loading question…
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.
…
…
…
…
…
…
…
…
…

40
9608/42/M/J/20 · Question 8May/June 2020Paper 423 marks
Loading question…
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

41
9608/21/O/N/19 · Question 4Oct/Nov 2019Paper 2111 marks
Loading question…
Question text

4
A student is developing a program to count how many times each character of the alphabet (A to Z)
occurs in a given string. Upper case and lower case characters will be counted as the same. The
string may contain non-alphabetic characters, which should be ignored.
The program will:
•
check each character in the string to count how many times each alphabetic character occurs
•
store the count for each alphabetic character in a 1D array
•
output each count together with the corresponding character.
(a) The student has written a structured English description of the algorithm:
1.
START at the beginning of the string
2.
SELECT a character from the string
3.
CONVERT the character to upper case
4.
CHECK whether the character is alphabetic and INCREMENT as required.
5.
REPEAT from step 2 until last character has been checked
6.
OUTPUT a suitable message giving the count of each alphabetic character
Step 4 above is not described in sufficient detail.
The student decides to apply a process to increase the level of detail given in step 4.
State the name of the process and use this process to write step 4 in more detail. Use
structured English for your answer.
Process …
Structured English …
…
…
…
…
…
[4]
(b) Write pseudocode to implement the program.
You should note the following:
•
InString contains the string to be checked. It has been assigned a value.
•
The elements of the array Result have all been initialised to zero.
•
The ASCII value of letter ‘A’ is 65.
You should assume the following lines of pseudocode have been written:
DECLARE InString : STRING
DECLARE Result : ARRAY [1:26] OF INTEGER
Declare any further variables you use. Do not implement the code as a subroutine.
Refer to the Appendix on pages 16–17 for the list of built-in functions and operators.
…
…
…
…
…
…
…
…
…

42
9608/21/O/N/19 · Question 6Oct/Nov 2019Paper 2121 marks
Loading question…
Question text

6
Account information for users of a library is held in one of two text files; UserListAtoM.txt and
UserListNtoZ.txt
The format of the data held in the two files is identical. Each line of the file is stored as a string that
contains an account number, name and telephone number separated by the asterisk character
('*') as follows:
<Account Number>'*'<Name>'*'<Telephone Number>
An example of one line from the file is:
"GB1234*Kevin Mapunga*07789123456"
The account number string may be six or nine characters in length and is unique for each
person. It is made up of alphabetic and numeric characters only.
An error has occurred and the same account number has been given to different users in the two
files. There is no duplication of account numbers within each individual file.
A program is to be written to search the two files and to identify duplicate entries. The account
number of any duplicate found is to be written to an array, Duplicates, which is a 1D array of
100 elements of data type STRING.
The program is to be implemented as several modules. The outline description of three of these is
as follows:
Module
Outline description
•
Initialise the global array Duplicates. Set all elements to the
ClearArray()
empty string.
•
Read each line from the file UserListAtoM.txt
Check whether the account number appears in file
•
UserListNtoZ.txt using SearchFileNtoZ()
FindDuplicates()
If the account number does appear then add the account
•
number to the array.
•
Output an error message and exit the module if there are more
duplicates than can be written to the array.
•
Search for a given account number in file UserListNtoZ.txt
SearchFileNtoZ()
If found, return TRUE, otherwise return FALSE
•
(a) State one reason for storing data in a file rather than in an array.
…
… [1]
(b) Write program code for the module SearchFileNtoZ().
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
…
…
…
…
…
…
…
…
…
…
…

43
9608/22/O/N/19 · Question 3Oct/Nov 2019Paper 2211 marks
Loading question…
Question text

3
A student is developing a program to search through a string of numeric digits to count how many
times each digit occurs. The variable InString will store the string and the 1D array Result will
store the count values.
The program will:
•
check each character in the string to count how many times each digit occurs
•
record the count for each digit using the array
•
output the count for each element of the array together with the corresponding digit.
(a) The array Result is a 1D array of type INTEGER.
Write pseudocode to declare the array and to initialise all elements to zero.
…
…
…
…
…
… [3]
(b) Write the pseudocode for the program.
Declare any variables you use. Do not implement the code as a subroutine.
Refer to the Appendix on page 18 for the list of built-in functions and operators.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

Advertisement
44
9608/22/O/N/19 · Question 6Oct/Nov 2019Paper 2218 marks
Loading question…
Question text

6
A text file, StudentList.txt, contains a list of information about students in a school.
Each line of the file contains a reference, name and date of birth for one student. All the information
is held as strings and separated by the asterisk character ('*') as follows:
<Reference>'*'<Name>'*'<Date Of Birth>
An example of one line from the file is:
"G1234*Aleza Hilton*05062001"
The reference string may be five or eight characters in length and is unique for each student. It is
made up of alphabetic and numeric characters only.
A global 1D array, Leavers, contains the references of all students who have recently left the
school. The array consists of 500 elements of data type STRING. Unused elements contain the
empty string "".
A program is to be written to produce a new text file, UpdatedList.txt, containing information
only for students who are still attending the school.
The program is to be implemented as several modules. The outline description of three of these is
as follows:
Module
Outline description
•
Read each line from the file StudentList.txt
Check whether the Reference appears in the
•
array using SearchLeavers()
ProcessStudentList()
If the Reference does not appear then write the
•
line to the file UpdatedList.txt
Return the number of lines not copied.
•
•
Search for a given Reference in the array Leavers
SearchLeavers()
•
If the Reference is found, return TRUE, otherwise
return FALSE
•
Take two parameters: the name of an array and a
string.
CountTimes()
•
Count the number of elements that are the same as
the string. Return the count value.
(a) Write program code for the module SearchLeavers(). Declare any additional variables
you use.
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
…
…
…
…
…
…
…
…
…
…
…
…
…
…

45
9608/23/O/N/19 · Question 6Oct/Nov 2019Paper 2318 marks
Loading question…
Question text

6
A text file, Library.txt, stores information relating to a book collection. The file stores four
pieces of information about each book on separate lines of the file, as follows:
Line
n:
<Book Title>
Line
n + 1:
<Author Name>
Line
n + 2:
<ISBN>
Line
n + 3:
<Location>
Information is stored as data strings.
Information relating to two books is shown:
File line
Data
100
"Learning Python"
101
"Brian Smith"
102
"978-14-56543-21-8"
103
"BD345"
104
"Surviving in the mountains"
105
"C T Snow"
106
"978-35-17635-43-9"
107
"ZX001"
(a) (i)
A function, FindBooksBy(), will search Library.txt for all books by a given author.
The function will store the Book Title and Location in the array Result, and will
return a count of the number of books found.
Array Result is a global 2D array of type STRING. It has 100 rows and 2 columns.
Write pseudocode to declare the array Result.
…
…
… [3]
(ii)
Function FindBooksBy() will:
•
receive the Author Name as a parameter
•
search Library.txt for matching entries
•
store the Book Title and Location of matching entries in the Result array
•
return an integer value giving the number of books by the author that were found.
Write program code for the function FindBooksBy().
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