Data types, records, arrays and abstract data types – A Level Computer Science (9618)
162 questions · page 8 of 11
Question text
2
A program contains an algorithm to output a string of a specified length containing identical
characters.
(a) The algorithm is described as follows:
1.
prompt and input a character and store in MyChar
2.
prompt and input an integer and store in MyCount
3.
generate a string consisting of MyChar repeated MyCount times
4.
output the string.
Draw a program flowchart to represent the algorithm.
START
END
[4]
(b) A different part of the program uses the variable StartDate.
Write pseudocode statements to declare StartDate and assign to it the date corresponding
to 15/11/2005.
Declaration …
Assignment …
[3]
Question text
3
Customers collect points every time they make a purchase at a store.
A program is used to manage the points system and the table lists some of the information stored
for one customer.
Information
Data type required
Name
String
Number of points collected
Integer
Date of birth
Date
(a) (i)
Identify a suitable structure for storing the information for one customer. Explain the
advantage of using this structure.
Structure …
Advantage …
…
…
…
…
…
[4]
(ii)
Describe a data structure that could be used to store the information for all customers.
…
… [2]
(b) Customers receive points depending on the amount they spend. The number of points
depends on the band that the amount falls into:
Band
Amount
Points
1
Less than $10
5 per whole dollar ($)
2
Between $10 and $100 inclusive
7 per whole dollar ($)
3
Over $100
10 per whole dollar ($)
For example, if the amount is $99.77, this amount is in band 2 and therefore the number of
points is 7 × 99, which is 693 points.
The algorithm to calculate the points from a given amount is expressed as follows:
•
work out the appropriate band
•
calculate and output the number of points.
Apply the process of stepwise refinement to increase the detail of the algorithm. Structure
your algorithm into a sequence of five steps that could be used to produce pseudocode.
Write the five steps.
1 …
…
…
2 …
…
…
3 …
…
…
4 …
Question text
6
A video-conferencing program supports up to six users. Speech from each user is sampled and
digitised (converted from analogue to digital). Digitised values are stored in array Sample.
The array Sample consists of 6 rows by 128 columns and is of type integer. Each row contains
128 digitised sound samples from one user.
The digitised sound samples from each user are to be processed to produce a single value which
will be stored in a 1D array Result of type integer. This process will be implemented by procedure
Mix().
A procedure Mix() will:
•
calculate the average of each of the 6 sound samples in a column
•
ignore sound sample values of 10 or less
•
store the average value in the corresponding position in Result
•
repeat for each column in array Sample
The diagram uses example values to illustrate the process:
2
3
...
126 127 128
1
20
20
20
30 30
2
1
20
20
30
50 30
3
2
20
20
40
40 40
4
3
Sample:
20
20
50
40 50 20
4
20
3
5
6
60
4
5
20
4
2
4
70 30
6
Result:
20
20
35
40
46
25
Write pseudocode for procedure Mix().
Assume Sample and Result are global.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
7
A school has a computerised library system that allows students to borrow books for a fixed length
of time. The system uses text files to store details of students, books and loans.
A new module is to be written which will generate emails to each student who has an overdue
book.
(a) Decomposition will be used to break down the problem of designing the new module into
sub-problems.
Identify three program modules that could be used in the design and describe their use.
Module 1 …
Use …
…
…
…
Module 2 …
Use …
…
…
…
Module 3 …
Use …
…
…
…
[3]
(b) The program designer produces a structure chart for the new module. Part of the structure
chart is shown:
Module-A()
Module-B()
Module-C()
Module-D()
(i)
Explain the relationship between the four modules shown.
…
…
…
… [2]
(ii)
Two new modules are added: Module‑X() and Module‑Y().
•
Module‑X() has no parameters.
•
Module‑Y() will take a string and a real number as parameters and return a
Boolean value.
•
Module‑D() will call either Module‑X() or Module‑Y().
Draw only the part of the structure chart that represents the relationship between
Module‑X(), Module‑Y() and Module‑D().
[3]
Question text
1
A program calculates the postal cost based on the weight of the item and its destination.
Calculations occur at various points in the program and these result in the choice of several
possible postal costs. The programmer has built these postal costs into the program.
For example, the postal cost of $3.75 is used in the following lines of pseudocode:
IF Weight < 250 AND ValidAddress = TRUE THEN
3.75
// set postal cost for item to $3.75
ItemPostalCost
"Valid"
// item can be sent
ItemStatus
ENDIF
(a) (i)
Identify a more appropriate way of representing the postal costs.
… [1]
(ii)
Describe the advantages of your answer to part (a)(i) with reference to this program.
…
…
…
…
…
… [3]
(b) The lines of pseudocode contain features that make them easier to understand.
State three of these features.
1 …
2 …
3 …
[3]
(c) Give the appropriate data types for the following variables:
ValidAddress …
ItemPostalCost …
ItemStatus …
[3]
Question text
2
A program stores a user’s date of birth using a variable MyDOB of type DATE.
(a) Write a pseudocode statement, using a function from the insert, to assign the value
corresponding to 17/11/2007 to MyDOB.
… [1]
(b) MyDOB has been assigned a valid value representing the user’s date of birth.
Write a pseudocode statement to calculate the number of months from the month of the
user’s birth until the end of the year and to assign this to the variable NumMonths.
For example, if MyDOB contains a value representing 02/07/2008, the value 5 would be
assigned to NumMonths.
… [2]
(c) The program will output the day of the week corresponding to MyDOB.
For example, given the date 22/06/2023, the program will output "Thursday".
An algorithm is required. An array will be used to store the names of the days of the week.
Define the array and describe the algorithm in four steps.
Do not use pseudocode statements in your answer.
Array definition …
…
Step 1 …
…
…
Step 2 …
…
…
Step 3 …
…
…
Step 4 …
…
…
[6]
Question text
3
A program stores data in a text file. When data is read from the file, it is placed in a queue.
(a) The diagram below represents an Abstract Data Type (ADT) implementation of the queue.
Each data item is stored in a separate location in the data structure. During initial design, the
queue is limited to holding a maximum of 10 data items.
The operation of this queue may be summarised as follows:
•
The Front of Queue Pointer points to the next data item to be removed.
•
The End of Queue Pointer points to the last data item added.
•
The queue is circular so that locations can be reused.
0
1
2
3
4
5
Front of Queue Pointer
Red
6
Green
7
Blue
8
End of Queue Pointer
Pink
9
(i)
Describe how the data items Orange and Yellow are added to the queue shown in the
diagram.
…
…
…
…
…
…
…
… [4]
(ii)
The following diagram shows the state of the queue after several operations have been
performed. All queue locations have been used at least once.
0
D4
1
End of Queue Pointer
D3
2
D27
3
D8
4
D33
5
Front of Queue Pointer
D17
6
D2
7
D1
8
D45
9
D60
State the number of data items in the queue.
… [1]
(b) The design of the queue is completed and the number of locations is increased.
A function AddToQueue() has been written. It takes a string as a parameter and adds this to
the queue. The function will return TRUE if the string was added successfully.
A procedure FileToQueue() will add each line from the file to the queue. This procedure
will end when all lines have been added or when the queue is full.
Describe the algorithm for procedure FileToQueue().
Do not use pseudocode in your answer.
…
…
…
…
…
…
…
…
Question text
1
The following pseudocode represents part of the algorithm for a program.
Line numbers are for reference only.
10
DECLARE Sheet4 : ARRAY[1:2, 1:50] OF INTEGER
…
100
FOR PCount
0 TO 49
0
101
Sheet4[1, PCount]
47
102
Sheet4[2, PCount]
103
NEXT PCount
(a) The pseudocode contains references to an array.
Complete the table by writing the answer for each row.
Answer
The dimension of the array
The name of the variable used as an array index
The number of elements in the array
[3]
(b) The pseudocode contains two errors. One error is that variable PCount has not been
declared.
Identify the other error and state the line number where it occurs.
Error …
…
…
Line number …
[2]
(c) The pseudocode does not include a declaration for PCount.
State the data type that should be used in the declaration.
… [1]
(d) The pseudocode statements given in the following table are used in other parts of the
algorithm.
Complete the table by placing one or more ticks (✓) in each row.
The first row has already been completed.
Pseudocode statement
Input
Process
Output
INPUT MyChoice
✓
OUTPUT FirstName & LastName
WRITEFILE YourFile, TextLine
READFILE MyFile, TextLine
SQRT(NextNum)
Result
[4]
Question text
3
A program processes data using a stack. The data is copied to a text file before the program ends.
(a) The following diagram shows the current state of the stack.
The operation of this stack may be summarised as follows:
•
The TopOfStack pointer points to the last item added to the stack.
•
The BottomOfStack pointer points to the first item on the stack.
•
The stack grows upwards when items are added.
Stack
Pointer
Memory location
Value
506
505
WWW
TopOfStack
504
YYY
503
XXX
502
ZZZ
501
NNN
500
PPP
BottomOfStack
(i)
An error will be generated if an attempt is made to POP a value when the stack is empty.
State the maximum number of consecutive POP operations that could be performed on
the stack shown above before an error is generated.
… [1]
(ii)
The following operations are performed:
1.
POP and store value in variable Data1
2.
POP and store value in variable Data2
3.
PUSH value AAA
4.
PUSH value BBB
5.
POP and discard value
6.
POP and store value in variable Data2
Complete the diagram to show the state of the stack and the variables after the given
operations have been performed.
Stack
Pointer
Memory location
Value
506
505
504
503
Variable
Value
502
Data1
501
Data2
500
[4]
(b) The data is copied to a text file before the program ends.
(i)
State an advantage of writing the data from the stack to a text file before the program
ends.
…
… [1]
(ii)
A module SaveStack() will write the data from the stack to a text file.
Express an algorithm for SaveStack() as five steps that could be used to produce
pseudocode.
Write the five steps.
Step 1 …
…
Step 2 …
…
Step 3 …
…
Step 4 …
…
Step 5 …
…
[5]
Question text
8
A computer shop assembles desktop computers, using items bought from several suppliers. A text
file Stock.txt contains information about each item.
Information for each item is stored as a single line in the Stock.txt file in the format:
<ItemNum><SupplierCode><Description>
Item information is as follows:
Format
Comment
unique number for each item in the range “0001”
ItemNum
4 numeric characters
to “5999” inclusive
SupplierCode
3 alphabetic characters
code to identify the supplier of the item
Description
a string
a minimum of 12 characters
The file is organised in ascending order of ItemNum and does not contain all possible values in
the range.
The programmer has defined the first program module as follows:
Module
Description
ChangeSupp()
• called with two parameters Code1 and Code2 of type string that represent
valid supplier codes
• creates a new file NewStock.txt from the contents of the
file Stock.txt where any reference to Code1 is replaced by Code2
• returns a count of the number of items that have had their supplier code
changed
(a) Write pseudocode for module ChangeSupp().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
A program reads data from a file and searches for specific data.
(a) The main program needs to read 25 integer data items from the text file Data.txt into a
local 1D array, DataArray
(i)
Write program code to declare the local array DataArray
Save your program as Question1_J2023.
Copy and paste the program code into part 1(a)(i) in the evidence document.
[1]
(ii)
Amend the main program to read the contents of Data.txt into DataArray
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
[4]
(b) (i)
The procedure PrintArray() takes an integer array as a parameter and outputs the
contents of the array in the order they are stored.
The items are printed on the same line, for example:
10
4
5
13
25
Write program code for the procedure PrintArray()
Save your program.
Copy and paste the program code into part 1(b)(i) in the evidence document.
[3]
(ii)
Amend the main program to output the contents of DataArray using the procedure
PrintArray()
Save your program.
Copy and paste the program code into part 1(b)(ii) in the evidence document.
[1]
(iii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(b)(iii) in the evidence document.
[1]
(c) The function LinearSearch():
•
takes an integer array and integer search value as parameters
•
counts and returns the number of times the search value is found in the array.
Write program code for the function LinearSearch()
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[3]
(d) (i)
Amend the main program to:
•
prompt the user to input a whole number between 0 and 100 inclusive
•
read and validate the input from the user
•
call LinearSearch() with DataArray and the validated input value
•
output the result in the format:
The number 7 is found 2 times.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[4]
(ii)
Test your program by inputting the number 12.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]
Question text
3
A program implements two stacks using 1D arrays. One stack stores the names of colours. One
stack stores the names of animals.
(a) The program contains the following global arrays and variables:
•
1D array Animal to store the names of up to 20 animals.
•
1D array Colour to store the names of up to 10 colours.
•
AnimalTopPointer to point to the next free space in the array Animal, initialised to 0.
•
ColourTopPointer to point to the next free space in the array Colour, initialised to 0.
Write program code to declare the global arrays and variables.
Save your program as Question3_J2023.
Copy and paste the program code into part 3(a) in the evidence document.
[3]
(b) (i)
Study the pseudocode function PushAnimal():
FUNCTION PushAnimal(DataToPush : STRING) RETURNS BOOLEAN
IF AnimalTopPointer = 20 THEN
RETURN FALSE
ELSE
DataToPush
Animal[AnimalTopPointer]
AnimalTopPointer + 1
AnimalTopPointer
RETURN TRUE
ENDIF
ENDFUNCTION
Write program code for the function PushAnimal()
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
[3]
(ii)
Study the pseudocode function PopAnimal():
FUNCTION PopAnimal() RETURNS STRING
DECLARE ReturnData : STRING
IF AnimalTopPointer = 0 THEN
RETURN ""
ELSE
Animal[AnimalTopPointer - 1]
ReturnData
AnimalTopPointer - 1
AnimalTopPointer
RETURN ReturnData
ENDIF
ENDFUNCTION
Write program code to declare the function PopAnimal()
Save your program.
Copy and paste the program code into part 3(b)(ii) in the evidence document.
[3]
(iii)
The procedure ReadData():
•
reads the animal names from the file AnimalData.txt
•
uses PushAnimal() to insert each name onto the stack
•
uses appropriate exception handling if the file does not exist.
Write program code for the procedure ReadData()
Save your program.
Copy and paste the program code into part 3(b)(iii) in the evidence document.
[5]
(iv)
The function PushColour() performs the same actions as PushAnimal() but inserts
an item into Colour.
The function PopColour() performs the same actions as PopAnimal() but removes
the next item from Colour.
Write program code for the functions PushColour() and PopColour()
Save your program.
Copy and paste the program code into part 3(b)(iv) in the evidence document.
[2]
(v)
Amend the procedure ReadData() so that it also:
•
reads the colours from the text file ColourData.txt
•
uses PushColour() to insert each colour onto the stack
•
uses appropriate exception handling if the file does not exist.
Save your program.
Copy and paste the program code into part 3(b)(v) in the evidence document.
[2]
(c) The procedure OutputItem():
•
pops the next item from both Animal and Colour
•
outputs the colour and animal on one line, for example "black horse"
If there is no data in Colour:
•
the animal is pushed back onto Animal
•
"No colour" is output.
If there is no data in Animal:
•
the colour is pushed back onto Colour
•
"No animal" is output.
Write program code for the procedure OutputItem()
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[5]
(d) The main program:
•
calls the procedure ReadData()
•
calls OutputItem() four times.
(i)
Write program code for the main program.
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[1]
(ii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 3(d)(ii) in the evidence document.
[1]
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 examinat
Question text
1
A 1D array needs to store the names of 10 animals.
(a) Write program code to declare the global string array Animals to store 10 items.
Save your program as Question1_J2023.
Copy and paste the program code into part 1(a) in the evidence document.
[2]
(b) The main program needs to store the following animals in the array:
horse
lion
rabbit
mouse
bird
deer
whale
elephant
kangaroo
tiger
Write program code to store these animal names in the array.
The names must be in lower case and stored in the order given in the list.
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[2]
(c) The following pseudocode procedure sorts the array into a descending alphabetical order
using only the first character in each animal name.
The function LENGTH(DataArray) returns the number of elements in the array DataArray.
The function MID(String, Start, Quantity) returns Quantity number of characters
from String starting at index Start. The first character in the string is index 0, for example:
MID("tiger", 0, 2) will return "ti"
There are four incomplete statements in the procedure.
PROCEDURE SortDescending()
DECLARE ArrayLength : INTEGER
DECLARE Temp : STRING
LENGTH(Animals)
ArrayLength
0 TO ArrayLength - 1
FOR X
… TO ArrayLength - X - 1
FOR Y
IF MID(Animals[Y], 0, 1) < MID(Animals[ … ], 0, 1) THEN
Animals[ … ]
Temp
Animals[Y + 1]
Animals[Y]
…
Animals[Y + 1]
ENDIF
NEXT Y
NEXT X
ENDPROCEDURE
Write program code for the procedure SortDescending().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[6]
(d) (i)
Write program code to amend the main program to:
•
call the procedure SortDescending()
•
output the sorted contents of the array with each animal name on a new line.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[3]
(ii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]
Question text
2
A business sells a single product. Customers can purchase one or more of this product.
Each sale has an ID and a quantity, for example "ABC" and 2
The business needs a program to store the data about the sales in a circular queue data structure.
(a) Write program code to declare a record structure, SaleData, to store the data about each
sale.
Save your program as Question2_J2023.
Copy and paste the program code into part 2(a) in the evidence document.
[2]
(b) Write program code to:
•
declare a global array, CircularQueue, of 5 items to store the sale records
•
declare the global pointers Head and Tail
•
declare the global variable NumberOfItems
•
initialise all elements of the array CircularQueue to an empty record, where the ID is
null ("") and quantity is -1
•
initialise Head, Tail and NumberOfItems to 0
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[4]
(c) The function Enqueue():
•
takes a new record as a parameter
•
inserts the record in the circular queue at the element pointed to by Tail
•
updates pointers and other variables as required
•
returns −1 if the circular queue is full
•
returns 1 if the record is stored successfully.
Write program code for the function Enqueue().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[6]
(d) The function Dequeue():
•
returns a null or empty record if the circular queue is empty
•
returns the first record in the queue if the circular queue is not empty
•
updates pointers and other variables as required.
Write program code for the function Dequeue().
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[6]
(e) The procedure EnterRecord():
•
takes an ID and quantity as input and creates a sale record
•
uses Enqueue() to insert the record in the circular queue
•
outputs "Full" if the record was not inserted in the circular queue
•
outputs "Stored" if the record was inserted in the circular queue.
Write program code for the procedure EnterRecord().
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[5]
(f)
The following sale records need to be entered into the program:
ID
Quantity
ADF
10
OOP
1
BXW
5
XXZ
22
HQR
6
LLP
3
(i)
Amend the main program to:
•
use EnterRecord() to input the six records in the table
•
use Dequeue() to remove one record
•
output either the ID and quantity of the removed record, or an error message if the
circular queue is empty
•
use EnterRecord() to input the record with the ID "LLP" for a second time
•
output the ID and quantity for all the records currently stored in the array
CircularQueue.
Write program code to perform these tasks.
Save your program.
Copy and paste the program code into part 2(f)(i) in the evidence document.
[4]
(ii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 2(f)(ii) in the evidence document.
[1]
Question text
1
(a) An algorithm includes a number of complex calculations. A programmer is writing a program
to implement the algorithm and decides to use library routines to provide part of the solution.
State three possible benefits of using library routines in the development of the program.
1 …
…
2 …
…
3 …
…
[3]
(b) The following pseudocode is part of a program that stores names and test marks for use in
other parts of the program.
DECLARE Name1, Name2, Name3 : STRING
DECLARE Mark1, Mark2, Mark3 : INTEGER
INPUT Name1
INPUT Mark1
INPUT Name2
INPUT Mark2
INPUT Name3
INPUT Mark3
(i)
The pseudocode needs to be changed to allow for data to be stored for up to 30 students.
Explain why it would be good practice to use arrays to store the data.
…
…
…
…
…
… [3]
(ii)
The following pseudocode statement includes array references:
OUTPUT "Student ", Name[Count], " scored ", Mark[Count]
State the purpose of the variable Count and give its data type.
Purpose …
…
Data type …
[2]
(c) The pseudocode statements in the following table may contain errors.
State the error in each case or write ‘NO ERROR’ if the statement contains no error.
Assume that any variables used are of the correct type for the given function.
Statement
Error
← "" THEN
IF EMPTY
← IS_NUM(-23.4)
Status
← STR_TO_NUM("37") + 5
X
← STR_TO_NUM("37" + "5")
Y
[4]