Data types, records, arrays and abstract data types – A Level Computer Science (9618)
162 questions · page 9 of 11
Question text
3
A 1D array Data of type integer contains 200 elements. Each element has a unique value.
An algorithm is required to search for the largest value and output it.
Describe the steps that the algorithm should perform.
Do not include pseudocode statements in your answer.
…
…
…
…
…
…
…
…
…
…
…
… [5]
Question text
4
(a) The following diagram shows an Abstract Data Type (ADT) representation of an ordered
linked list. The data item stored in each node is a single character. The data will be accessed
in alphabetical order.
The symbol Ø represents a null pointer.
Start pointer
'C'
'J'
'L'
Ø
(i)
Nodes with data 'A' and 'K' are added to the linked list. Nodes with data 'J' and 'L' are
deleted.
After the changes, the data items still need to be accessed in alphabetical order.
Complete the diagram to show the new state of the linked list.
Start pointer
'C'
'J'
'L'
[4]
(ii)
The original data could have been stored in a 1D array in which each element stores a
character.
For example:
'C'
'J'
'L'
Explain the advantages of making the changes described in part (a)(i) when the data is
stored in the linked list instead of an array.
…
…
…
… [2]
(iii)
Explain the disadvantages of making the changes described in part (a)(i) when the data
is stored in the linked list instead of an array.
…
…
…
… [2]
(b) A program will store data using a linked list like the one shown in part (a).
Explain how the linked list can be implemented.
…
…
…
…
…
…
…
… [4]
Question text
5
A program uses two 1D arrays of type integer. Array1 contains 600 elements and Array2
contains 200 elements.
Array1 contains sample values read from a sensor. The sensor always takes three consecutive
samples and all of these values are stored in Array1.
A procedure Summarise() will calculate the average of three consecutive values from Array1
and write the result to Array2. This will be repeated for all values in Array1.
The diagram below illustrates the process for the first six entries in Array1.
}
Array1
Array2
Comment
15
10
average of first three values
20
41
average of next three values
}
15
40
41
42
Write pseudocode for the procedure Summarise().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
Question text
2
(a) An algorithm will process data from a test taken by a group of students. The algorithm will
prompt and input the name and test mark for each of the 35 students.
The algorithm will add the names of all the students with a test mark of less than 20 to an
existing text file Support_List.txt, which already contains data from other group tests.
(i)
Describe the steps that the algorithm should perform.
Do not include pseudocode statements in your answer.
…
…
…
…
…
…
…
…
…
…
…
… [5]
(ii)
Explain why it may be better to store the names of the students in a file rather than in an
array.
…
…
… [1]
(iii)
Explain why WRITE mode cannot be used in the answer to part 2(a)(i).
…
…
… [1]
(b) Examine the following state-transition diagram.
Input-A Output-X
Input-B Output-W
Input-B
START
S2
S1
S3
Input-A
Input-A
Input-B
Input-A Output-W
S4
Complete the table to show the inputs, outputs and next states.
Input
Output
Next state
S1
Input-A
S2
Output-W
Output-W
[4]
Question text
3
A stack is used in a program to store string data which needs to be accessed in several modules.
(a) A stack is an example of an Abstract Data Type (ADT).
Identify one other example of an ADT and describe its main features.
Example …
Features …
…
…
…
…
…
[3]
(b) Explain how the stack can be implemented using an array.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
(c) A second stack is used in the program. The diagram below shows the initial state of this
stack. Value X is at the top of the stack and was the last item added.
Upper-case letters are used to represent different data values.
Stack operations are performed in three groups as follows:
Group 1:
PUSH D
PUSH E
Group 2:
POP
POP
POP
Group 3:
PUSH A
PUSH B
POP
PUSH C
Complete the diagram to show the state of the stack after each group of operat
Question text
7
A teacher is designing a program to perform simple syntax checks on programs written by
students.
Two global 1D arrays are used to store the syntax error data. Both arrays contain 500 elements.
•
Array ErrCode contains integer values that represent an error number in the range 1 to 800.
•
Array ErrText contains string values that represent an error description.
The following diagram shows an example of the arrays.
ErrCode
ErrText
Index
10
"Invalid identifier name"
1
20
"Bracket mismatch"
2
50
"Undeclared variable"
3
60
"Type mismatch in assignment"
4
…
999
500
<Undefined>
Note:
•
There may be less than 500 error numbers so corresponding elements in both arrays may be
unused. Unused elements in ErrCode have the value 999. The value of unused elements in
ErrText is undefined.
•
Values in the ErrCode array are stored in ascending order but not all values may be present,
for example, there may be no error code 31.
The teacher has defined two program modules as follows:
Module
Description
•
takes two parameters as integers:
○
a line number in the student’s program
○
an error number
•
searches for the error number in the ErrCode array:
OutputError()
○
if found, outputs the corresponding error description and the
line number, for example:
"Bracket mismatch on line 34"
○
if not found, outputs the line number and a warning, for
example:
"Unknown error on line 34"
SortArrays()
sorts the arrays into ascending order of ErrCode
(a) Write efficient pseudocode for module OutputError().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
(b) Write an efficient bubble sort algorithm in pseudocode for module SortArrays().
…
Question text
4
(a) A program contains a 1D array DataItem with 100 elements.
State the one additional piece of information required before the array can be declared.
…
… [1]
(b) A programmer decides to implement a queue Abstract Data Type (ADT) in order to store
characters received from the keyboard. The queue will need to store at least 10 characters
and will be implemented using an array.
(i)
Describe two operations that are typically required when implementing a queue.
State the check that must be carried out before each operation can be completed.
Operation 1 …
…
Check 1 …
…
Operation 2 …
…
Check 2 …
…
[4]
(ii)
Describe the declaration and initialisation of the variables and data structures used to
implement the queue.
…
…
…
…
…
…
…
…
…
…
… [5]
Question text
7
A teacher is designing a program to perform simple syntax checks on programs written by
students.
Two global 1D arrays are used to store the syntax error data. Both arrays contain 500 elements.
•
Array ErrCode contains integer values that represent an error number in the range 1 to 800.
•
Array ErrText contains string values that represent an error description.
The following diagram shows an example of the arrays.
ErrCode
ErrText
Index
10
"Invalid identifier name"
1
20
"Bracket mismatch"
2
50
""
3
60
"Type mismatch in assignment"
4
...
999
<Undefined>
500
Note:
•
There are less than 500 error codes so corresponding elements in both arrays may be
unused. Unused elements in ErrCode have the value 999. These will occur at the end of the
array. The value of unused elements in ErrText is undefined.
•
Values in the ErrCode array are stored in ascending order but not all values may be present.
For example, there may be no error code 31.
•
Some error numbers are undefined. In these instances, the ErrCode array will contain a
valid error number but the corresponding ErrText element will contain an empty string.
The teacher has defined one program module as follows:
Module
Description
• Prompts for input of two error numbers
• Outputs a list of error numbers between the two numbers input
(inclusive) together with the corresponding error description
• Outputs a warning message when the error description is
missing as for error number 50 in the example
• Outputs a suitable header and a final count of error numbers
found
OutputRange()
Output based on the example array data above:
List of error numbers from 1 to 60
10
:
Invalid identifier name
20
:
Bracket mismatch
50
:
Error Text Missing
60
:
Type mismatch in assignment
4 error numbers output
(a) Write pseudocode for module OutputRange(). Assume that the two numbers input
represent a valid error number range.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
The text file IntegerData.txt stores 100 integer numbers between 1 and 100 inclusive. A
program is required to read in this data and perform searching and sorting on the data.
(a) Write program code to declare a global 1D array, DataArray, with space for 100 integer
values.
Save your program as Question1_N22.
Copy and paste the program code into part 1(a) in the evidence document.
[2]
(b) The procedure ReadFile() must read in the numbers from the text file and store each one
in the array. Use appropriate exception handling.
Write program code for the procedure ReadFile().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[6]
(c) The function FindValues() asks the user to enter a number to search for in the array.
The number input must be a whole number between 1 and 100 inclusive. The function then
returns the number of times the number input appears in the array.
Write program code for the function FindValues().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[7]
(d) (i)
Write program code to call ReadFile() and FindValues() from the main program.
The return value from FindValues() must be output with an appropriate message.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
[3]
(ii)
Test your program using the number 61 as input.
Take a screenshot to show the output.
Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]
(e) The procedure BubbleSort() needs to perform a bubble sort on the array and print the
contents of the sorted array.
Write program code for the procedure BubbleSort() and call it from the main program.
Save your program.
Copy and paste the program code into part 1(e) in the evidence document.
[4]
Question text
3
A binary tree consists of nodes. Each node has 3 integer values: a left pointer, data and a right
pointer.
The binary tree is stored using a global 2D array.
The pseudocode declaration for the array is:
DECLARE ArrayNodes : ARRAY[0:19, 0:2] OF INTEGER
For example:
•
ArrayNodes[0, 0] stores the left pointer for the first node.
•
ArrayNodes[0, 1] stores the data for the first node.
•
ArrayNodes[0, 2] stores the right pointer for the first node.
–1 indicates a null pointer, or null data.
(a) Write program code to:
•
declare the global 2D array ArrayNodes
•
initialise all 3 integer values to –1 for each node.
Save your program as Question3_N22.
Copy and paste the program code into part 3(a) in the evidence document.
[3]
(b) The binary tree stores the following values:
Index
Left pointer
Data
Right pointer
0
1
20
5
1
2
15
–1
2
–1
3
3
3
–1
9
4
4
–1
10
–1
5
–1
58
–1
6
–1
–1
–1
FreeNode stores the index of the first free element in the array, initialised to 6.
RootPointer stores the index of the first node in the tree, initialised to 0.
Amend your program by writing program code to store the given data in ArrayNodes and
initialise the free node and root node pointers.
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[2]
(c) The following recursive pseudocode function searches the binary tree for a given value. If the
value is found, the function must return the index of the value. If the value is not found, the
function must return –1.
The function is incomplete. There are four incomplete statements.
FUNCTION SearchValue(Root : INTEGER,
ValueToFind : INTEGER) RETURNS INTEGER
IF Root = –1 THEN
RETURN –1
ELSE
IF ArrayNodes[Root, 1] = ValueToFind THEN
RETURN …
ELSE
IF ArrayNodes[Root, 1] = –1 THEN
RETURN –1
ENDIF
ENDIF
ENDIF
IF ArrayNodes[Root, 1] … ValueToFind THEN
RETURN SearchValue(ArrayNodes[ … , 0], ValueToFind)
ENDIF
IF ArrayNodes[Root, … ] < ValueToFind THEN
RETURN SearchValue(ArrayNodes[Root, 2], ValueToFind)
ENDIF
ENDFUNCTION
Write program code for the function SearchValue().
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[5]
(d) A post order traversal performs the following operation:
•
visit the left node
•
visit the right node
•
output the root.
For example, in the following tree, the output would be: 3
9
25
60
50
50
25
60
3
9
An outline of the PostOrder() procedure is:
•
If left node is not empty, make a recursive call with the left node as the root.
•
If right node is not empty, make a recursive call with the right node as the root.
•
Output the current root node.
The procedure PostOrder() takes the root node as a parameter.
Write program code for the procedure PostOrder().
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[7]
(e) (i)
Amend the main program by writing program code to:
•
call the function SearchValue() to find the position of the number 15 in the tree
•
use the result from SearchValue() to output either the index of the value if found,
or an appropriate message to state that the value was not found
•
call the procedure PostOrder().
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[3]
(ii)
Test your program.
Take a screenshot to show the output.
Copy and paste the screenshot into part 3(e)(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.
Question text
1
A computer program is needed to store jobs in order of priority. Each job has a job number
(for example, 123) and a priority from 1 to 10, with 1 being the highest priority and 10 the lowest.
The program stores the jobs in a global 2D array.
The pseudocode declaration for the array is:
DECLARE Jobs : ARRAY[0:99, 0:1] OF INTEGER
For example:
•
Jobs[0, 0] stores the job number of the first job.
•
Jobs[0, 1] stores the priority of the first job.
The global variable, NumberOfJobs, stores the number of jobs currently in the array.
(a) Write program code to declare the global 2D array Jobs and the global variable
NumberOfJobs.
Save your program as Question1_N22.
Copy and paste the program code into part 1(a) in the evidence document.
[3]
(b) The procedure Initialise() stores –1 in each of the array elements and assigns 0 to
NumberOfJobs.
Write program code for the procedure Initialise().
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[3]
(c) The procedure AddJob():
•
takes a job number and priority as parameters
•
stores the job in the next free array element
•
outputs ‘Added’ if the job was successfully stored in the array
•
outputs ‘Not added’ if the job was not successfully stored in the array.
Write program code for the procedure AddJob().
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
[5]
(d) The main program should call the procedure Initialise() and then use the AddJob()
procedure to store the following jobs in the order given:
Job number
Priority
12
10
526
9
33
8
12
9
78
1
Write program code for the main program and perform the tasks described.
Save your program.
Copy and paste the program code into part 1(d) in the evidence document.
[2]
(e) When a new job has been added, the array is sorted into ascending numerical order of priority
using an insertion sort.
Write program code for the procedure InsertionSort() to sort the data into ascending
numerical order of priority.
Save your program.
Copy and paste the program code into part 1(e) in the evidence document.
[5]
(f)
The procedure PrintArray() outputs each job number and priority on a line, for example:
123 priority 1
39 priority 3
120 priority 7
Write program code for the procedure PrintArray().
Save your program.
Copy and paste the program code into part 1(f) in the evidence document.
[3]
(g) The main program needs to sort the array and then output the contents of the array.
(i)
Amend the main program by writing program code to call procedures InsertionSort()
and PrintArray().
Save your program.
Copy and paste the program code into part 1(g)(i) in the evidence document.
[1]
(ii)
Test your program.
Take a screenshot of the output.
Copy and paste the screenshot into part 1(g)(ii) in the evidence document.
[1]
Question text
2
A computer game is being developed. The game has 10 different characters that are all active in
the game.
Part of the game is being written using object-oriented programming.
The class Character stores data about the characters. Each character has a name and the x
coordinate and y coordinate of their current position.
Character
Name : STRING
stores the name of the character
XCoordinate : INTEGER
stores the x coordinate
YCoordinate : INTEGER
stores the y coordinate
Constructor()
initialises Name, XCoordinate and YCoordinate from the
values passed as parameters
GetName()
returns the name of the character
GetX()
returns the x coordinate of the character
GetY()
returns the y coordinate of the character
ChangePosition()
takes XChange as an integer parameter and adds it to the
x coordinate
takes YChange as an integer parameter and adds it to the
y coordinate
(a) Write program code to declare the class Character and its constructor. Do not write
program code for the other methods.
Use your programming language appropriate constructor.
All attributes must be private. If you are writing in Python, include attribute declarations using
comments.
Save your program as Question2_N22.
Copy and paste the program code into part 2(a) in the evidence document.
[4]
(b) Write program code for the three get methods for the class Character.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[3]
(c) Write program code for the method ChangePosition().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) The main program has a 1D array of characters. Each character is stored as an object of type
Character.
The game has a maximum of 10 characters. The character names, x coordinates and y
coordinates are stored in the file Characters.txt in the order:
•
name
•
x coordinate
•
y coordinate.
For example, the first character in the file is named Amal, with the x coordinate 0 and the y
coordinate 2.
Amend the main program by writing program code to:
•
declare the array
•
read in all 10 characters from Characters.txt
•
store each character as an object in the array.
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[7]
(e) The main program needs to read in a character’s name from the user, search for the character
in the array and store the index of its position. It repeats until the user enters a name that
exists in the array.
Amend the main program by writing program code to perform this task.
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[5]
(f)
The user will enter a letter to identify the direction the chosen character from part 2(e) should
move.
•
If ‘A’ is input, the character moves left (x coordinate minus 1).
•
If ‘W’ is input, the character moves up (y coordinate plus 1).
•
If ‘S’ is input, the character moves down (y coordinate minus 1).
•
If ‘D’ is input, the character moves right (x coordinate plus 1).
Amend the main program by writing program code to:
•
take a letter as input until it is a valid move (A, W, S or D)
•
change the position of the character using the appropriate method.
Save your program.
Copy and paste the program code into part 2(f) in the evidence document.
[7]
(g) (i)
When a change to a character’s position has been made, the program needs to output
the character’s name and the new x and y coordinates of the character, in the format:
Qui has changed coordinates to X = 83 and Y = 0
Amend the main program by writing program code to perform these tasks.
Save your program.
Copy and paste the program code into part 2(g)(i) in the evidence document.
[2]
(ii)
Test your program by inputting the following four items of data in the order given:
THOMAS
qui
X
A
Take a screenshot of the output.
Copy and paste the screenshot into part 2(g)(ii) in the evidence document.
[1]
Question text
3
A program uses a linear queue to store up to 100 integers.
(a) A 1D array, Queue, is used to store the data. The head pointer points to the first number
stored in the queue and the tail pointer points to the next free space in the queue.
Write program code to:
•
declare the global array Queue
•
declare the global variable head pointer and assign an appropriate initial value
•
declare the global variable tail pointer and assign an appropriate initial value.
Save your program as Question3_N22.
Copy and paste the program code into part 3(a) in the evidence document.
[3]
(b) The function Enqueue() takes an integer value as a parameter and stores it in the queue. It
returns TRUE if the value was successfully stored and FALSE otherwise.
Write program code for the function Enqueue().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[6]
(c) The main program uses the Enqueue() function to store the numbers 1 to 20 (inclusive)
in the queue, in ascending numerical order. The program should output ‘Successful’ if all
numbers are successfully enqueued, and ‘Unsuccessful’ otherwise.
Amend the main program by writing program code to perform this task.
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[4]
(d) The following iterative pseudocode function calculates the total of all the values stored in the
queue.
FUNCTION IterativeOutput(Start: INTEGER) RETURNS INTEGER
DECLARE Total : INTEGER
← 0
Total
FOR Count ← Start - 1 TO HeadPointer STEP -1
Total ← Total + Queue[Count]
NEXT Count
RETURN Total
ENDFUNCTION
Rewrite the function as a recursive function using program code.
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[6]
(e) The main program calls the recursive function from part 3(d) and outputs the value returned.
(i)
Amend the main program by writing program code to perform this task.
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[1]
(ii)
Test your program.
Take a screenshot to show the output.
Copy and paste the screenshot into part 3(e)(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 examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
Question text
4
A stack is created using a high-level language. Memory locations 200 to 207 are to be used to
store the stack.
The following diagram represents the current state of the stack.
TopOfStack points to the last value added to the stack.
Stack
Pointer
Memory
Value
location
200
201
202
203
'F'
TopOfStack
204
'C'
205
'D'
206
'E'
207
'H'
(a) Complete the following table by writing the answers.
Answer
The value that has been on the stack for the longest time.
The memory location pointed to by TopOfStack if three POP
operations are performed.
[2]
(b) The following diagram shows the current state of the stack:
Stack
Pointer
Memory
Value
location
200
201
202
'W'
TopOfStack
203
'Y'
204
'X'
205
'Z'
206
'N'
207
'P'
The following operations are performed:
POP
POP
PUSH 'A'
PUSH 'B'
POP
PUSH 'C'
PUSH 'D'
Complete the diagram to show the state of the stack after the operations have been
performed.
Stack
Pointer
Memory
Value
location
200
201
202
203
204
205
206
207
[4]
Question text
5
Each line of a text file contains data organised into fixed-length fields as shown:
<Field 1><Field 2><Field 3>
An algorithm is required to search for the first instance of a given value of Field 2 and, if found, to
output the corresponding values for Field 1 and Field 3.
Describe the algorithm needed.
Do not include pseudocode statements in your answer.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]