Programming – A Level Computer Science (9618)

153 questions · page 10 of 11

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
136
9618/23/M/J/22 · Question 4May/June 2022Paper 234 marks
Loading question…
Question text

4
Items in a factory are weighed automatically. The weight is stored as an integer value representing
the item weight to the nearest gram (g).
A function is written to validate the weight of each item. The function will return "PASS" if the
weight of the item is within the acceptable range, otherwise the function will return "FAIL".
The acceptable weight range for an item is 150 g to 155 g inclusive.
The validation function is to be properly tested. Black-box testing will be used and a test plan
needs to be produced.
Complete the table by writing additional tests to test this function.
Type of
Example
Expected
Explanation
test data
test value
return value
Normal
153
"PASS"
Value within the acceptable range
[4]

137
9618/23/M/J/22 · Question 6May/June 2022Paper 2310 marks
Loading question…
Question text

6
(a) The factorial of a number is the product of all the integers from 1 to that number.
For example:
factorial of 5 is given by 1 × 2 × 3 × 4 × 5 = 120
factorial of 7 is given by 1 × 2 × 3 × 4 × 5 × 6 × 7 = 5040
factorial of 1 = 1
Note: factorial of 0 = 1
A function Factorial() will:
•
be called with an integer number as a parameter
•
calculate and return the factorial of the number
•
return −1 if the number is negative.
Write pseudocode for the function Factorial().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
(b) A procedure FirstTen() will output the factorial of the numbers from 0 to 9. The procedure
will use the function from part (a).
The required output is:
Factorial of 0 is 1
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 9 is 362880
The program flowchart represents an algorithm for FirstTen().
START
Set Num to 0
C
A
F
B
D
E
END
Complete the table by writing the text that should replace each label A to F.
Label
Text
A
B
C
D
E
F
[4]

138
9618/23/M/J/22 · Question 7May/June 2022Paper 238 marks
Loading question…
Question text

7
The following pseudocode represents an algorithm intended to output the last three lines as they
appear in a text file. Line numbers are provided for reference only.
10
PROCEDURE LastLines(ThisFile : STRING)
11
DECLARE ThisLine : STRING
12
DECLARE Buffer : ARRAY[1:3] OF STRING
13
DECLARE LineNum : INTEGER
1
14
LineNum
15
OPENFILE ThisFile FOR READ
16
17
WHILE NOT EOF(ThisFile)
18
READFILE Thisfile, ThisLine // read a line
ThisLine
19
Buffer[LineNum]
LineNum + 1
20
LineNum
21
IF LineNum = 4 THEN
1
22
LineNum
23
ENDIF
24
ENDWHILE
25
26
CLOSEFILE ThisFile
1 TO 3
27
FOR LineNum
28
OUTPUT Buffer[LineNum]
29
NEXT LineNum
30
ENDPROCEDURE
(a) There is an error in the algorithm. In certain cases, a text file will have at least three lines but
the output will be incorrect.
(i)
State how the output may be incorrect.
…
… [1]
(ii)
Describe the error in the algorithm and explain how it may be corrected.
Description …
…
…
…
Explanation …
…
…
…
[4]
(b) The original algorithm is implemented and sometimes the last three lines of the text file are
output correctly.
State the condition that results in the correct output.
…
… [1]
(c) Lines 20 to 23 inclusive could be replaced with a single pseudocode statement.
Write the pseudocode statement.
…
… [2]

Advertisement
139
9618/23/M/J/22 · Question 9May/June 2022Paper 2316 marks
Loading question…
Question text

9
A program allows a user to save passwords used to log in to websites. A stored password is then
inserted automatically when the user logs in to the corresponding website.
A student is developing a program to generate a strong password. The password will be of a fixed
format, consisting of three groups of four alphanumeric characters, separated by the hyphen
character '-'.
An example of a password is: "FxAf-3hzV-Aq49"
A valid password:
•
must be 14 characters long
•
must be organised as three groups of four alphanumeric characters. The groups are
separated by hyphen characters
•
may include duplicated characters, provided these appear in different groups.
The programmer has started to define program modules as follows:
Module
Description
• Generates a single random character from within one of the following
ranges:
○ 'a' to 'z'
RandomChar()
○ 'A' to 'Z'
○ '0' to '9'
• Returns the character
• Takes two parameters:
○ a string
○ a character
Exists()
• Performs a case-sensitive search for the character in the string
• Returns TRUE if the character occurs in the string, otherwise returns
FALSE
• Generates a valid password
• Uses RandomChar() and Exists()
Generate()
• Returns the password
Note: in a case-sensitive comparison, 'a' is not the same as 'A'.
(a) Write pseudocode for the module Generate().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

140
9618/21/O/N/21 · Question 5Oct/Nov 2021Paper 2110 marks
Loading question…
Question text

5
The following data items will be recorded each time a student successfully logs on to the school
network:
Data item
Example data
"CJL404"
Student ID
"Lib01"
Host ID
"08:30, June 01, 2021"
Time and date
The Student ID is six characters long. The other two data items are of variable length.
A single string will be formed by concatenating the three data items. A separator character will
need to be inserted between items two and three.
For example:
"CJL404Lib01<separator>08:30, June 01, 2021"
Each string represents one log entry.
A programmer decides to store the concatenated strings in a 1D array LogArray that contains
2000 elements. Unused array elements will contain an empty string.
(a) Suggest a suitable separator character and give a reason for your choice.
Character …
Reason …
…
[2]
(b) The choice of data structure was made during one stage of the program development life
cycle.
Identify this stage.
… [1]
(c) A function LogEvents() will:
•
take a Student ID as a parameter
•
for each element in the array that matches the Student ID parameter:
◦ add the value of the array element to the existing text file LogFile
◦ assign an empty string to the array element
•
count the number of lines added to the file
•
return this count.
Write pseudocode for the function LogEvents().
…
…
…
…
…
…
…
…
…
…
…
…
…
…

141
9618/21/O/N/21 · Question 6Oct/Nov 2021Paper 2119 marks
Loading question…
Question text

6
A mobile phone has a touchscreen. The screen is represented by a grid, divided into 800 rows
and 1280 columns.
The grid is represented by a 2D array Screen of type INTEGER. An array element will be set to 0
unless the user touches that part of the screen.
Many array elements are set to 1 by a single touch of a finger or a stylus.
The following diagram shows a simplified touchscreen. The dark line represents a touch to the
screen. All grid elements that are wholly or partly inside the outline will be set to 1. These elements
are shaded.
The element shaded in black represents the centre point.
11
6
A program is needed to find the coordinates (the row and column) of the centre point. The centre
point on the diagram above is row 6, column 11.
Assume:
•
the user may only touch one area at a time
•
screen rotation does not affect the touchscreen.
The programmer has started to define program modules as follows:
Module
Description
• Called with three parameters of type INTEGER:
◦
a row number
SetRow()
◦
the number of pixels to be skipped starting from column 1
(generates test
◦
the number of pixels that should be set to 1
data)
• Sets the required number of pixels to 1
For example, SetRow(3, 8, 5) will give row 3 as in the diagram shown.
• Takes two parameters of type INTEGER:
◦
a row number
◦
a start column (1 or 1280)
• Searches the given row from the start column (either left to right or right to left)
SearchInRow()
for the first column that contains an element set to 1
• Returns the column number of the first element in the given row that is set to 1
• Returns −1 if no element is set to 1
• Takes two parameters of type INTEGER:
◦
a column number
◦
a start row (1 or 800)
• Searches the given column from the start row (either up or down) for the first
SearchInCol()
row that contains an element set to 1
• Returns the row number of the first element in the given column that is set to 1
• Returns −1 if no element is set to 1
(a) Write pseudocode to implement the module SetRow().
…
…
…
…
…
…
…
…
…
…
…
… [5]
(b) The module description of SearchInRow() is provided here for reference.
Module
Description
• Takes two parameters of type INTEGER:
◦
a row number
◦
a start column (1 or 1280)

142
9618/22/O/N/21 · Question 4Oct/Nov 2021Paper 2211 marks
Loading question…
Question text

4
The following is a procedure design in pseudocode.
Line numbers are given for reference only.
10
PROCEDURE Check(InString : STRING)
11
DECLARE Odds, Evens, Index : INTEGER
12
← 0
13
Odds
← 0
14
Evens
← 1
15
Index
16
17
WHILE Index <= LENGTH(InString)
18
IF STR_TO_NUM(MID(InString, Index, 1)) MOD 2 <> 0 THEN
←
Odds + 1
19
Odds
20
ELSE
←
Evens + 1
21
Evens
22
ENDIF
←
Index + 1
23
Index
24
ENDWHILE
25
26
CALL Result(Odds, Evens)
27
ENDPROCEDURE
(a) Complete the following table by giving the answers, using the given pseudocode.
Answer
A line number containing a variable being incremented
The type of loop structure
The number of functions used
The number of parameters passed to STR_TO_NUM()
The name of a procedure other than Check()
[5]
(b) The pseudocode includes several features that make it easier to read and understand.
Identify three of these features.
1 …
2 …
3 …
[3]
(c) (i)
The loop structure used in the pseudocode is not the most appropriate.
State a more appropriate loop structure and justify your choice.
Loop structure …
Justification …
…
…
[2]
(ii)
The appropriate loop structure is now used. Two lines of pseudocode are changed and
two lines are removed.
Write the line numbers of the two lines that are removed.
…
… [1]

143
9618/22/O/N/21 · Question 5Oct/Nov 2021Paper 2210 marks
Loading question…
Question text

5
A company has several departments. Each department stores the name, email address and the
status of each employee in that department in its own text file. All text files have the same format.
Employee details are stored as three separate data strings on three consecutive lines of the file.
An example of the first six lines of one of the files is as follows:
File line
Comment
1
First employee name
2
First email address
3
First employee status
4
Second employee name
5
Second email address
6
Second employee status
A procedure MakeNewFile() will:
•
take three parameters as strings:
○ an existing file name
○ a new file name
○ a search status value
•
create a new text file using the new file name
•
write all employee details to the new file where the employee status is not equal to the search
status value
•
count the number of sets of employee details that were in the original file
•
count the number of sets of employee details that were written to the new file
•
produce a summary output.
An example summary output is as follows:
File Marketing contained 54 employee details
52 employee sets of details were written to file NewMarketingList
(a) Write pseudocode for the procedure MakeNewFile().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

Advertisement
144
9618/22/O/N/21 · Question 6Oct/Nov 2021Paper 2219 marks
Loading question…
Question text

6
A mobile phone has a touchscreen. The screen is represented by a grid, divided into 800 rows
and 1280 columns.
The grid is represented by a 2D array Screen of type INTEGER. An array element will be set to 0
unless the user touches that part of the screen.
Many array elements will set to 1 by a single touch of a finger or a stylus.
The following diagram shows a simplified touchscreen. The dark line represents a touch on the
screen. All grid elements that are wholly or partly inside the outline will be set to 1. These elements
are shaded.
The element shaded in black represents the centre point of the touch.
11
6
A program is needed to find the coordinates (the row and column) of the centre point. The centre
point on the diagram shown is row 6, column 11.
Assume:
•
the user may only touch one area at a time
•
screen rotation does not affect the touchscreen.
The programmer has decided to use global values CentreRow and CentreCol as coordinate
values for the centre point.
The programmer has started to define program modules as follows:
Module
Description
• Searches for the first row that has an array element set to 1
• Returns the index of that row (1 is the first row)
FirstRowSet()
• Returns −1 if there are no elements set to 1
• Searches for the last row that has an array element set to 1
• Returns the index of that row
LastRowSet()
• Returns −1 if there are no elements set to 1
• Searches for the first column that has an array element set to 1
• Returns the index of that column (1 is the first column)
FirstColSet()
• Returns −1 if there are no elements set to 1
• Searches for the last column that has an array element set to 1
• Returns the index of that column
LastColSet()
• Returns −1 if there are no elements set to 1
(a) Write efficient pseudocode for the module FirstRowSet().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

145
9618/21/M/J/21 · Question 1May/June 2021Paper 2112 marks
Loading question…
Question text

1
(a) A program is being developed to help manage the membership of a football club.
Complete the following identifier table.
Example
Explanation
Variable name
Data type
value
The preferred name of the member
"Wong"
joining the football club
A value to indicate whether an
FALSE
existing member of the club lives at
the same address
When the member joined the football
19/02/1983
club
The number of points a member has
1345
earned. Members of the club earn
points for different activities.
[4]
(b) Each pseudocode statement in the following table may contain an error due to the incorrect
use of the function or operator.
Describe the error in each case, or write ‘NO ERROR’ if the statement contains no error.
You can assume that none of the variables referenced are of an incorrect type.
Statement
Error
2 & 4
Result
MID("pseudocode", 4, 1)
SubString
IF x = 3 OR 4 THEN
Status AND INT(x/2)
Result
"Done" + LENGTH(MyString)
Message
[5]
(c) The following data items need to be stored for each student in a group:
•
student name (a string)
•
test score (an integer).
State a suitable data structure and justify your answer.
Structure …
Justification …
…
…
[3]

146
9618/21/M/J/21 · Question 2May/June 2021Paper 2113 marks
Loading question…
Question text

2
(a) Four program modules form part of a program for a library.
A description of the relationship between the modules is summarised as follows:
Module name
Description
•
UpdateLoan()
Calls either LoanExtend() or LoanReturn()
•
Called with parameters LoanID and BookID
•
Calls CheckReserve() to see whether the book has been
LoanExtend()
reserved for another library user
•
Returns TRUE if the loan has been extended, otherwise returns
FALSE
•
Called with BookID
•
CheckReserve()
Returns TRUE if the book has been reserved, otherwise returns
FALSE
•
Called with parameters LoanID and BookID
•
LoanReturn()
Returns a REAL (which is the value of the fine to be paid in the
case of an overdue loan)
Draw a structure chart to show the relationship between the four modules and the parameters
passed between them.
[5]
(b) The definition for module LoanReturn() is amended as follows:
Module name
Description
Called with parameters LoanID, BookID and Fine
LoanReturn()
The module code checks whether the book has been returned on time
and then assigns a new value to Fine
•
LoanID and BookID are of type STRING
•
Fine is of type REAL
Write the pseudocode header for the amended module LoanReturn().
…
… [2]
(c) A program will:
•
input 50 unique integer values
•
output the largest value
•
output the average of the values excluding the largest value.
Draw a program flowchart to represent the algorithm.
Variable declarations are not required.
It is not necessary to check that each input value is unique.
[6]

147
9618/21/M/J/21 · Question 4May/June 2021Paper 2110 marks
Loading question…
Question text

4
Study the following pseudocode. Line numbers are for reference only.
10 FUNCTION Convert(Name : STRING) RETURNS STRING
11
12
DECLARE Flag: BOOLEAN
13
DECLARE Index : INTEGER
14
DECLARE ThisChar : CHAR
15
DECLARE NewName : STRING
16
17
CONSTANT SPACECHAR = ' '
18
TRUE
19
Flag
1
20
Index
""
// formatted name string
21
NewName
22
23
WHILE Index <= LENGTH(Name)
MID(Name, Index, 1)
24
ThisChar
25
IF Flag = TRUE THEN
NewName & UCASE(ThisChar)
26
NewName
27
IF ThisChar <> SPACECHAR THEN
FALSE
28
Flag
29
ENDIF
30
ELSE
NewName & ThisChar
31
NewName
32
ENDIF
33
IF ThisChar = SPACECHAR THEN
TRUE
34
Flag
35
ENDIF
Index + 1
36
Index
37
ENDWHILE
38
39
RETURN NewName
40
41 ENDFUNCTION
(a) Complete the trace table below by dry running the function when it is called as follows:
Convert("∇in∇a∇∇Cup")
Result
Note: The symbol '∇' has been used to represent a space character.
Use this symbol for any space characters in the trace table.
The first row has been completed for you.
Name
Flag
Index
NewName
ThisChar
"∇in∇a∇∇Cup"
[5]
(b) The pseudocode for Convert() contains a conditional loop.
State a more appropriate loop structure.
Justify your answer.
Loop structure …
…
Justification …
…
…
[2]
(c) Two changes need to be made to the algorithm.
Change 1:
Convert to lower case any character that is not the first character after a space.
Change 2:
Replace multiple spaces with a single space.
(i)
Change 1 may be implemented by modifying one line of the pseudocode.
Write the modified line.
…
… [1]
(ii)
Change 2 may be implemented by moving one line of the pseudocode.
Write the number of the line to be moved and state its new position.
Line number …
New position …
…
[2]

148
9618/21/M/J/21 · Question 7May/June 2021Paper 2116 marks
Loading question…
Question text

7
A program is needed to take a string containing a full name and produce a new string of initials.
Some words in the full name will be ignored. For example, "the", "and", "of", "for" and "to" may all
be ignored.
Each letter of the abbreviated string must be upper case.
For example:
Full name
Initials
Integrated Development Environment
IDE
The American Standard Code for Information Interchange
ASCII
The programmer has decided to use a global variable FNString of type STRING to store the full
name.
It is assumed that:
•
words in the full name string are separated by a single space character
•
space characters will not occur at the beginning or the end of the full name string
•
the full name string contains at least one word.
The programmer has started to define program modules as follows:
Module
Description
•
Called with an INTEGER as a parameter, representing the number of a
word in FNString.
•
Returns the character start position of that word in FNString or
GetStart()
returns –1 if that word does not exist
•
For example: if FNString contains the string "hot and cold",
GetStart(3) returns 9
•
Called with a parameter representing the position of the first character
of a word in FNString
•
GetWord()
Returns the word from FNString
•
For example: if FNString contains the string "hot and cold",
GetWord(9) returns "cold"
(a) Write pseudocode for the module GetStart().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

Advertisement
149
9618/22/M/J/21 · Question 1May/June 2021Paper 2211 marks
Loading question…
Question text

1
(a) (i)
Complete the following table by giving the appropriate data type in each case.
Variable
Example data value
Data type
Name
"Catherine"
Index
100
Modified
FALSE
Holiday
25/12/2020
[4]
(ii)
Evaluate each expression in the following table by using the initial data values shown in
part (a)(i).
Expression
Evaluates to
Modified OR Index > 100
LENGTH("Student: " & Name)
INT(Index + 2.9)
MID(Name, 1, 3)
[4]
(b) Each pseudocode statement in the following table contains an example of selection,
assignment or iteration.
Put one tick (‘✓’) in the appropriate column for each statement.
Statement
Selection
Assignment
Iteration
Index
Index + 1
IF Modified = TRUE THEN
ENDWHILE
[3]

150
9618/22/M/J/21 · Question 5May/June 2021Paper 2215 marks
Loading question…
Question text

5
(a) A student is learning about arrays.
She wants to write a program to:
•
declare a 1D array RNum of 100 elements of type INTEGER
•
assign each element a random value in the range 1 to 200 inclusive
•
count and output how many numbers generated were between 66 and 173 inclusive.
(i)
Write pseudocode to represent the algorithm.
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
(ii)
The student decides to modify the algorithm so that each element of the array will contain
a unique value.
Describe the changes that the student needs to make to the algorithm.
…
…
…
…
…
… [3]
(b) The following is a pseudocode function.
Line numbers are given for reference only.
01
FUNCTION StringClean(InString : STRING) RETURNS STRING
02
03
DECLARE NextChar : CHAR
04
DECLARE OutString : STRING
05
DECLARE Counter : INTEGER
06
""
07
OutString
08
1 TO LENGTH(InString)
09
FOR Counter
MID(InString, Counter, 1)
10
NextChar
LCASE(NextChar)
11
NextChar
12
IF NOT((NextChar < 'a') OR (NextChar > 'z')) THEN
OutString & NextChar
13
OutString
14
ENDIF
15
NEXT Counter
16
17
RETURN OutString
18
19
ENDFUNCTION
(i)
Examine the pseudocode and complete the following table.
Answer
Give a line number containing an example of an initialisation statement.
Give a line

Advertisement