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

118 questions · page 5 of 8

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
61
9608/21/O/N/18 · Question 4Oct/Nov 2018Paper 2120 marks
Loading question…
Question text

4
Part of a program written in pseudocode is shown.
01 DECLARE NumElements : INTEGER
…
10 FUNCTION ScanArray(SearchString : STRING) RETURNS INTEGER
11
12
DECLARE ArrayIndex : INTEGER
13
DECLARE ArrayString : STRING
14
DECLARE NumberFound : INTEGER
15
0
16
ArrayIndex
0
17
NumberFound
18
1 TO NumElements
19
FOR ArrayIndex
ResultArray[ArrayIndex, 1]
20
ArrayString
21
IF ArrayString = SearchString
22
THEN
23
CALL SaveToFile(ArrayString)
NumberFound + 1
24
NumberFound
25
ENDIF
26
ENDFOR
27
28
RETURN NumberFound
29
30 ENDFUNCTION
(a) (i)
Examine the pseudocode and complete the following table.
Answer
The identifier name of a global integer
The identifier name of a user-defined procedure
The line number of an unnecessary statement
The scope of ArrayString
[4]
(ii)
Describe in detail the purpose of lines 19 to 26 in the function ScanArray().
Do not use pseudocode in your answer.
…
…
…
…
…
…
… [4]
(b) The function ScanArray() needs to be amended so that the comparison is not case
sensitive. For example, comparing “Aaaa” with “AAAa” should evaluate to TRUE.
Write program code to implement the amended ScanArray() function.
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
…
…
…
…
…
…
…
…
…
…
…

62
9608/22/O/N/18 · Question 1Oct/Nov 2018Paper 2216 marks
Loading question…
Question text

1
(a) The following table contains statements written in pseudocode.
Show the type of programming construct each statement represents.
Put a tick (3) in the appropriate column for each statement.
Repetition
Statement
Selection
Assignment
(Iteration)
Index + 5
Index
1 TO 100
FOR Count
ReadValue(SensorID)
TempValue[Index]
IF Index < 30
UNTIL DayNumber > 7
OTHERWISE OUTPUT "ERROR"
[6]
(b) (i)
The following table contains statements written in pseudocode.
Give the most appropriate data type for the variable used in each statement.
Statement
Data type
Revision
'B'
13.3
MaxValue
TRUE
ArrayFull
"Design"
Activity
270
NumberOfEdits
[5]
(ii)
The following table contains statements written in pseudocode.
Complete the table by evaluating each expression using the values from part (b)(i).
If any expression is invalid, write “ERROR” in the Evaluates to column.
For the built-in functions list, refer to the Appendix on page 16.
Expression
Evaluates to
MID(Activity, 3, 4) & "ature"
INT(MaxValue * 2)
ArrayFull AND NumberOfEdits < 300
ASC(Revision + 1)
Activity = "Testing" OR Revision = 'A'
[5]

63
9608/22/O/N/18 · Question 3Oct/Nov 2018Paper 228 marks
Loading question…
Question text

3
(a) Programming is sometimes referred to as a transferable skill.
You are asked to work on a program written in a language you are not familiar with.
Explain how transferable skills would help you work on the program.
…
…
…
… [2]
(b) Stepwise refinement is often used in the development of an algorithm.
Describe stepwise refinement.
…
…
…
… [2]
(c) A program needs to search through 1000 elements of an unsorted array to find a given value.
The program will output:
•
either the position in the array of the value
•
or the message “Not Found”
Outline the steps the program needs to follow.
Do not write pseudocode or program code.
…
…
…
…
…
…
…
…
…
…
… [4]
Question 4 begins on the next page.

Advertisement
64
9608/22/O/N/18 · Question 4Oct/Nov 2018Paper 2222 marks
Loading question…
Question text

4
Part of a program written in pseudocode is shown.
010 DECLARE NextArrayElement : INTEGER
…
100 FUNCTION ScanFile(SearchString: STRING) RETURNS INTEGER
101
102
DECLARE FileData : STRING
103
DECLARE FileLine : INTEGER
104
105
NextArrayElement
1
106
FileLine
1
107
FileData
ReadFileLine("DataFile.txt", FileLine)
108
109
WHILE FileData <> ""
110
IF LEFT(FileData, 7) = SearchString
111
THEN
112
ResultArray[NextArrayElement]
FileData
113
NextArrayElement
NextArrayElement + 1
114
ENDIF
115
FileLine
FileLine + 1
116
FileData
ReadFileLine("DataFile.txt", FileLine)
117
ENDWHILE
118
119
CALL ScanCompleted()
120
RETURN FileLine
121
122 ENDFUNCTION
(a) (i)
Examine the pseudocode. Complete the following table.
Answer
The identifier name of a local variable
The identifier name of a user-defined procedure
The identifier name of a user-defined function
The number of dimensions of ResultArray
The scope of FileData
[5]
(ii)
Describe in detail the purpose of lines 109 to 117 in the ScanFile() function. Do not
use pseudocode in your answer.
…
…
…
…
…
… [4]
(b) The function ScanFile() is to be amended so that the first 7 characters of FileData are
not written to the array.
Write program code to implement this amended ScanFile() function.
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
…
…
…
…
…
…
…
…
…
…
…

65
9608/22/O/N/18 · Question 5Oct/Nov 2018Paper 2211 marks
Loading question…
Question text

5
The procedure LineNumber() will:
•
read data from a text file
•
modify each line by adding a line number, and the string (": ")
•
output each modified line.
For example, when the procedure reads MyFile.txt, the output is:
10: <First line of MyFile.txt>
15: <Second line of MyFile.txt>
20: <Third line of MyFile.txt>
…
350: <Last line of MyFile.txt>
The procedure takes three parameters:
Identifier
Data type
Description
FileName
The name of the text file
STRING
StartNumber
The first line number to be added
INTEGER
StepNumber
The line number increment
INTEGER
In this example, the procedure call would be:
CALL LineNumber("MyFile.txt", 10, 5)
After every 20 lines, the program outputs a message asking whether the user wants to continue.
The program ends when the user enters an 'N' or the end of file is reached.
Write pseudocode for the LineNumber() procedure.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

66
9608/23/O/N/18 · Question 3Oct/Nov 2018Paper 235 marks
Loading question…
Question text

3
An array contains 100 integer values. An algorithm will find the maximum and minimum values
stored in the array.
(a) A programmer has started to write this program using a conditional loop.
Name a more appropriate loop structure for this task and justify your choice.
Name …
Justification …
…
…
[2]
(b) Outline the steps the program will need to follow to implement the algorithm.
Do not write pseudocode or program code.
…
…
…
…
…
…
…
… [3]
Question 4 begins on the next page.

67
9608/23/O/N/18 · Question 5Oct/Nov 2018Paper 2310 marks
Loading question…
Question text

5
The function ReadFileLine() returns a specific line from a text file.
The function takes two parameters:
Identifier
Data type
Description
The name of the text file
FileName
STRING
The line number that is required
FileLine
INTEGER
The following pseudocode gives an example of the use of the function.
ReadFileLine(FileName, FileLine)
FileData
The function ReadFileLine() will:
•
open the file, FileName
•
read each line from the file until line FileLine is found or the end of the file is reached
•
if the line exists, return the string from this line; otherwise return the string "****"
Write pseudocode for the ReadFileLine() function.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

68
9608/41/O/N/18 · Question 2Oct/Nov 2018Paper 4116 marks
Loading question…
Question text

2
Kendra collects books. She is writing a program to store and analyse information about her books.
Her program stores information about each book as a record. The following table shows the
information that will be stored about each book.
Field name
Description
The title of the book
Title
The first listed author of the book
Author
A 13-digit code that uniquely identifies the book,
for example:
ISBN
"0081107546738"
If the book is fiction (TRUE) or non-fiction (FALSE)
Fiction
The date when Kendra last read the book
LastRead
(a) Write pseudocode to declare an Abstract Data Type (ADT) named Book, to store the
information in the table.
…
…
…
…
…
…
…
…
… [4]
(b) The records are stored in a random access file.
The function, Hash(), takes as a parameter the ISBN and returns the hash value.
The disk address of the record in the hash table is calculated as: ISBN modulus 2000 plus 1.
Write program code for the function Hash().
Programming language …
Program code …
…
…
…
…
…
…
… [4]
(c) The random access file, MyBooks.dat, stores the data about the books in the format:
<Title>
<Author>
<ISBN>
<Fiction>
<LastRead>
A procedure, FindBook():
prompts the user to input the ISBN of a book until the ISBN contains 13 numeric digits
•
uses the function Hash() to calculate the disk address of the record
•
reads the record for that book from MyBooks.dat into a variable of type Book
•
outputs all the data

Advertisement
69
9608/41/O/N/18 · Question 3Oct/Nov 2018Paper 4113 marks
Loading question…
Question text

3
Joseph is taking a toy apart. Each time he removes an item from the toy, he writes the name of
the item at the bottom of a paper list. When he rebuilds the toy, he puts the items back together
working from the bottom of the list.
Joseph writes a computer program to create the list using a stack, Parts.
(a) Describe a stack structure.
…
… [1]
(b) The stack is represented as an array in the program, the first element in the array is [0].
The current contents of the stack, Parts, and its pointer, StackPointer are shown.
StackPointer
StackContents
5
0
"Screw 1"
1
"Screw 2"
2
"Back case"
3
"Screw 3"
4
"Engine outer"
5
6
7
(i)
Describe the purpose of the variable StackPointer.
…
… [1]
(ii)
The procedure POP() removes an item from the stack. The procedure
PUSH(<identifier>) adds an item to the stack.
The current contents of the stack, Parts, and its pointer, StackPointer are shown.
StackPointer
StackContents
5
0
"Screw 1"
1
"Screw 2"
2
"Back case"
3
"Screw 3"
4
"Engine outer"
5
6
7
Use the table below to show the contents of the stack, Parts, and its pointer after the
following code is run.
POP()
POP()
PUSH("Light 1")
PUSH("Light 2")
PUSH("Wheel 1")
POP()
POP()
StackPointer
StackContents
0
1
2
3
4
5
6
7
[2]
(c) A 1D array, Parts, is used to implement the stack. Parts is declared as:
DECLARE Parts : ARRAY[0 : 19] OF STRING
(i)
The procedure POP outputs the last element that has been pushed onto the stack and
replaces it with a '*'.
Complete the pseudocode for the procedure POP.
PROCEDURE POP
IF … . = … .
THEN
OUTPUT "The stack is empty"
ELSE
…
StackPointer
OUTPUT … . … .
… .
Parts[StackPointer]
ENDIF
ENDPROCEDURE
[5]
(ii)
The procedure PUSH() puts the parameter onto the stack.
Complete the pseudocode for the procedure PUSH().
PROCEDURE PUSH(BYVALUE Value : String)
IF StackPointer > … .…
THEN
OUTPUT "Stack full"
ELSE
… . … .…..
… . … .. …
… .…. … .
StackPointer
ENDIF
ENDPROCEDURE
[4]

70
9608/41/O/N/18 · Question 4Oct/Nov 2018Paper 4115 marks
Loading question…
Question text

4
The recursive algorithm for the Calculate() function is defined as follows:
01 FUNCTION Calculate(BYVALUE Number : INTEGER) RETURNS INTEGER
02
IF Number = 0
03
THEN
−10
04
Calculate
05
ELSE
Number * Calculate(Number − 1)
06
Calculate
07
ENDIF
08 ENDFUNCTION
(a) (i)
State what is meant by a recursive algorithm.
…
… [1]
(ii)
State the line number in Calculate() where the recursive call takes place.
… [1]
Question 4(b) begins on the next page.
(b) The function is called with Calculate(3).
Dry run the function and complete the trace table below. State the final value returned. Show
your working.
01 FUNCTION Calculate(BYVALUE Number : INTEGER) RETURNS INTEGER
02
IF Number = 0
03
THEN
−10
04
Calculate
05
ELSE
Number * Calculate(Number − 1)
06
Calculate
07
ENDIF
08 ENDFUNCTION
Working …
…
…
…
Trace table:
Number = 0 ?
Call number
Function call
Return value
Final return value …
[6]
(c) A recursive algorithm within a subroutine can be replaced with an iterative algorithm.
(i)
Describe one problem that can occur when running a subroutine that has a recursive
algorithm.
…
…
…
… [2]
(ii)
Rewrite the Calculate() function in pseudocode, using an iterative algorithm.
…
…
…
…
…
…
…
…
…

71
9608/42/O/N/18 · Question 3Oct/Nov 2018Paper 4213 marks
Loading question…
Question text

3
A bubble sort algorithm is used to sort an integer array, List. This algorithm can process arrays
of different lengths.
(a) Write pseudocode to complete the bubble sort algorithm shown.
… TO 0 STEP - 1
01 FOR Outer
0 TO ( … )
02
FOR Inner
IF …
…
03
>
04
THEN
…
05
Temp
…
06
List[Inner]
…
07
List[Inner + 1]
08
ENDIF
09
ENDFOR
10 ENDFOR
[7]
(b) (i)
State the order of the sorted array.
… [1]
(ii)
State which line of the algorithm you would change to sort the array into the opposite
order.
State the change you would make.
Line …
Change …
…
[1]
(c) Use pseudocode to write an alternative version of this bubble sort algorithm that will exit the
algorithm when the list is fully sorted.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

72
9608/42/O/N/18 · Question 6Oct/Nov 2018Paper 4210 marks
Loading question…
Question text

6
A linked list abstract data type (ADT) is created. This is implemented as an array of records. The
records are of type ListElement.
An example of a record of ListElement is shown in the following table.
Data Item
Value
Country
"Scotland"
Pointer
1
(a) (i)
Use pseudocode to write a definition for the record type, ListElement.
…
…
…
…
… [3]
(ii)
Use pseudocode to write an array declaration to reserve space for only 15 nodes of
type ListElement in an array, CountryList. The lower bound element is 1.
… [2]
(b) The program stores the position of the last node in the linked list in LastNode. The last node
always has a Pointer value of -1. The position of the node at the head of the list is stored in
ListHead.
After some processing, the array and variables are in the following state.
CountryList
ListHead
Country
Pointer
1
1
"Wales"
2
LastNode
2
"Scotland"
4
3
3
-1
4
"England"
5
5
"Brazil"
6
6
"Canada"
7
7
"Mexico"
8
8
"Peru"
9
9
"China"
10
10
11
11
12
12
13
13
14
14
15
15
3
A recursive algorithm searches the list for a value, deletes that value, and updates the
required pointers. When a node value is deleted, it is set to empty "" and the node is added
to the end of the list.
A node value is deleted using the pseudocode statement
CALL DeleteNode("England", 1, 0)
Complete the following pseudocode to implement the DeleteNode procedure.
PROCEDURE DeleteNode(NodeValue: STRING, ThisPointer : INTEGER,
PreviousPointer : INTEGER)
IF CountryList[ThisPointer].Value = NodeValue
THEN
""
CountryList[ThisPointer].Value
IF ListHead = …
THEN
ListHead
…
ELSE
CountryList[PreviousPointer].Pointer
CountryList[ThisPointer].Pointer
ENDIF
CountryList[LastNode].Pointer
…
LastNode
ThisPointer
…
ELSE
IF CountryList[ThisPointer].Pointer <> -1
THEN
CALL DeleteNode(NodeValue, … ,
ThisPointer)
ELSE
OUTPUT "DOES NOT EXIST"
ENDIF
ENDIF
ENDPROCEDURE
[5]
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 International
Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after
the live examination series.
Cambridge International Examinations is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of University of Cambridge Local

73
9608/21/M/J/18 · Question 1May/June 2018Paper 2114 marks
Loading question…
Question text

(a) A program stores data about hospital patients.
1
Give a suitable identifier name for each of the data items.
Description of data item
Suitable identifier name
The temperature of the patient
The temperature of the room
The patient identification number
The name of the nurse taking the measurement
[4]
Program variables have values as follows:
(b) (i)
Variable
Value
MyGreeting
"Happy Birthday"
MyInitial
'C'
AgeInYears
27
Weight
60.5
Married
TRUE
Children
TRUE
Evaluate each expression in the following table.
If an expression is invalid, write ERROR.
For the built-in functions list, refer to the Appendix on page 15.
Expression
Evaluates to
"Mon" & MID(MyGreeting, 10, 2)
AgeInYears + ASC(MyInitial)
INT(MyInitial)
MOD(Weight * 2, 10)
Married AND (NOT Children)
[5]
Programming languages support different data types.
(ii)
Give an appropriate data type for each of these variables from part (b)(i).
Variable
Data type
MyGreeting
MyInitial
AgeInYears
Weight
Married
[5]

Advertisement
74
9608/21/M/J/18 · Question 5May/June 2018Paper 2112 marks
Loading question…
Question text

A golf club holds information about its members. When a member completes a round of golf, their
5
score is stored along with their membership number and the date of the round.
(a) Explain why the club stores these data in a file rather than an array.
…
… [1]
(b) Editing functions such as cut, copy and paste are features provided by an Integrated
Development Environment (IDE).
Give two additional features of an IDE that are helpful when coding a program.
Feature 1 …
…
Feature 2 …
…
[2]
(c) The information is stored in a text file, ScoreDetails.txt. The format of each line of the
text file is as follows:
<MembershipNumber><Date><Score>
•
MembershipNumber is a four-digit numeric string.
•
Date is a six-digit numeric string in the format DDMMYY
•
Score is a two-digit numeric string in the range "50" to "99".
A procedure, AddNewScores(), is being developed. This will allow the user to enter scores
for several members on a particular date.
The procedure, AddNewScores(), will perform the following actions:
1.
Prompt for the date of the scores.
2.
Input the date of the scores.
3.
Prompt for the membership number.
4.
Input the membership number.
5.
If the membership number is an empty string then end the procedure.
6.
Prompt for the score.
7.
Input the score.
8.
Validate the score.
9.
If the validation fails then repeat from step 6.
10.
Form a text string from the data and write this to the ScoreDetails.txt file.
11.
Repeat from step 3.
Write program code for the AddNewScores() procedure.
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
…
…
…
…
…
…
…
…
…

75
9608/21/M/J/18 · Question 6May/June 2018Paper 2111 marks
Loading question…
Question text

6
(a) The following pseudocode includes references to a 1D array.
DECLARE StudentGrade : ARRAY[1:5] OF CHAR
DECLARE n : INTEGER
DECLARE x : CHAR
3
n
StudentGrade[n]
x
(i)
Use the correct technical terms to explain the meaning of [1:5] in this pseudocode.
…
…
…
… [2]
(ii)
Use the correct technical term to complete the following statement.
Integer n is used as the … to StudentGrade.
[1]
(b) A 2D array, Picture, contains data representing a bitmap image. Each element of the array
represents one pixel of the image. The image is grey-scale encoded where the value of each
pixel ranges from 0 (representing black) to 255 (representing white) with intermediate values
representing different levels of grey.
The following is an example of an image and the corresponding data values for the Picture
array.
Bitmap image
Values
240 10
10
10
10
10
10 240
80
80 240 80
80 240 80
80
80
80 240 80
80 240 80
80
80
80 150 150 150 150 80
80
80
80 240 240 240 240 80
80
80
80 150 150 150 150 80
80
240 240 150 150 150 150 240 240
240 240 150 150 150 150 240 240
In pseudocode, the array is declared as follows:
DECLARE Picture : ARRAY[1:8, 1:8] OF INTEGER
A function, Lighten(), is required to lighten the image. Lightening an image may cause it to
‘burn out’. An image is said to be ‘burnt out’ if any pixel is set to the maximum value of 255.
The function Lighten() will:
1.
increase the value of each pixel by 10%
2.
return TRUE if the resultant image is ‘burnt out’.
Write pseudocode to implement the Lighten() function.
Assume that the array Picture is a global variable.
…
…
…
…
…
…
…
…
…
…
…
…

Advertisement