Programming – O Level Computer Science (2210)
174 questions · page 3 of 12
Question text
4
A telephone number with 13 characters is input. The first character is a ‘+’, followed by a 2-digit
country code, for example:
+20xxxxxxxxxx where 20 is the country code and x is a digit between 0 and 9 inclusive.
The algorithm includes checks for the country codes: 44 for the UK and 20 for Egypt.
(a) Write an algorithm using pseudocode to:
•
input the telephone number
•
extract the 2-digit country code
•
output the matching country
•
output ‘Unknown’ if there is no matching country.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
ĬÍĊ®Ġ´íÈõÏĪÅĊàû·Ā×
,
,
(b) Explain how you could change the algorithm to repeatedly prompt for the telephone number
until the number entered is 13 characters in length.
Any code used must be fully explained.
…
…
…
…
Question text
5
Two library routines used in programming are MOD and DIV.
State the purpose of each of the library routines.
Give one example pseudocode statement for each of the library routines.
MOD purpose …
…
Example pseudocode …
…
DIV purpose …
…
Example pseudocode …
…
[4]
ĬÏĊ®Ġ´íÈõÏĪÅĊàù·Ā×
Question text
6
An algorithm has been written in pseudocode to generate a random integer between 1 and 100
inclusive and store the value in N. The user enters a guess for the number and the algorithm
indicates whether the guess is higher or lower than the random number. The algorithm outputs
the number of guesses when the user has correctly guessed the random number.
0
01
Counter
ROUND(RANDOM() * 99, 1) + 1
02
N
03
REPEAT
04
OUTPUT "Please enter a guess"
05
INPUT G
06
IF G > N
07
THEN
08
OUTPUT "The number is lower than your guess"
09
ELSE
10
IF G < N
11
THEN
12
OUTPUT "The number is lower than your guess"
13
ENDIF
Counter + 1
14
Counter
15
ENDIF
16
UNTIL G = N
17
OUTPUT "Well done, you took ", Guess, " attempts"
(a) Identify the line numbers of the four errors in the pseudocode and suggest a correction for
each error.
Error 1 line number …
Correction …
…
Error 2 line number …
Correction …
…
Error 3 line number …
Correction …
…
Error 4 line number …
Correction …
…
[4]
(b) Identify one method to make the algorithm maintainable.
Give an example.
…
…
…
… [2]
ĬÑĊ®Ġ´íÈõÏĪÅĊÝù¶þ×
Question text
9
The variables A and B are used in a program. A holds a whole number and B holds text.
(a) Write pseudocode statements to declare the variables A and B
…
…
… [2]
(b) The function Odds(X) returns ‘Odd’ if the value of X is an odd number and returns ‘Even’ if
the value of X is an even number.
Write pseudocode statements to:
•
define the function
•
call the function using parameter A and store the return item in variable B
Function definition …
…
…
…
…
…
…
…
…
…
…
Function call …
…
[6]
ĬÓĊ®Ġ´íÈõÏĪÅĊßû¶Ā×
Question text
11
The one-dimensional (1D) array CompetitorName[] contains the names of 25 competitors in a
tournament that includes five events.
Another two-dimensional (2D) array CompetitorScore[] contains the points scored by each
competitor in the five events. The first dimension of this array contains the index of the competitor
from CompetitorName[] and the second dimension contains their scores.
Each competitor receives a score out of 100 for each event. These scores are manually entered
into CompetitorScore[]
Medals are awarded to the competitor or the competitors who have:
•
the highest points scored in each of the five events
•
the highest total points scored for all five events.
The one-dimensional (1D) array Points[] stores the total number of points scored for each
competitor.
Write a program that meets the following requirements:
•
takes as input, valid scores (between 0 and 100), for each competitor in each of the five
events
•
calculates the highest points scored for each event
•
outputs the names of the competitors who receive a medal for the highest points scored
•
calculates the highest total points scored for the five events
•
outputs the names of the competitors with the highest total points scored for the five events.
You must use pseudocode or program code and add comments to explain how your code works.
You do not need to declare any arrays or variables.
All inputs and outputs must contain suitable messages.
…
…
…
…
…
…
…
…
…
…
…
…
ĬÑĊ®Ġ´íÈõÏĪÅĊàù¸Ā×
,
,
…
…
…
…
…
Question text
2
A fence measuring 2 metres in height and 20 metres in length needs painting. The area of the
fence needs to be calculated so that enough paint for the job is purchased.
After the task is decomposed, there are three component parts: input, process and output.
Three component parts and four example descriptions are shown.
Draw one line from each component part to the most appropriate relevant description.
Not all descriptions will be used.
Component part
Description
the height of the fence
the length of the fence
process
The amount of paint required is
40 square metres.
output
the colour of the paint
input
The area of fence is calculated as
40 square metres.
[3]
Question text
4
(a) State one reason for using a verification check when inputting data.
…
… [1]
(b) Write the pseudocode for an algorithm to perform a double entry verification check, using the
following criteria:
•
Input 10 numbers and store them in the array Numbers[]
•
Input the same 10 numbers again; as each number is input, compare it with its
corresponding number in the array Numbers[]
◦
if the numbers match, move on to the next number
◦
if the numbers do not match, display both numbers and ask for the number to be
re-entered
◦
store the re-entered number in the corresponding array location and move on to the
next number.
•
When all the numbers have been checked, display a message to say the check has
been completed.
You do not need to declare any arrays or variables for this algorithm.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
Question text
5
This pseudocode algorithm should allow 500 names of people and their heights to be entered and
stored in two one-dimensional (1D) arrays. The name of a person and the height of that person
are in the same index in both the Names[] and the Heights[] arrays.
The algorithm performs as follows:
•
at the start, the following data is entered:
◦
name of person
◦
height of person in cm
•
the data is stored in the relevant arrays
•
the heights of the people are totalled
•
the shortest person is found
•
at the end, the following is output:
◦
name of shortest person
◦
height of shortest person
◦
average height of all 500 people.
01
DECLARE Names : ARRAY[1:500] OF REAL
02
DECLARE Heights : ARRAY[1:500] OF REAL
03
DECLARE Shortest : REAL
04
DECLARE Total : REAL
05
DECLARE Counter : INTEGER
06
DECLARE Index : INTEGER
500
07
Shortest
0
08
Index
100
09
Total
1 TO 500
10
FOR Counter
11
INPUT Names[Index]
12
INPUT Heights[Counter]
Counter + Heights[Counter]
13
Total
14
IF Heights[Counter] < Shortest
15
THEN
Heights[Counter]
16
Shortest
Counter
17
Index
18
ENDIF
19
NEXT Counter
20
OUTPUT "The shortest height is ", Heights
21
OUTPUT "The shortest person is ", Names[Index]
22
OUTPUT "The average height is ", Total / 500
(a) Identify the line numbers of five errors in the pseudocode and suggest a correction for each
error.
Error 1 line number …
Correction …
…
Error 2 line number …
Correction …
…
Error 3 line number …
Correction …
…
Error 4 line number …
Correction …
…
Error 5 line number …
Correction …
…
[5]
(b) Write the pseudocode statement that will output the average height rounded to one decimal
place.
…
… [2]
(c) Explain how you could change the corrected algorithm so that it will also:
•
find the height of the tallest person.
•
output the height of the tallest person at the end of the algorithm.
Any program statements used must be fully explained.
…
…
.
Question text
6
This pseudocode is an algorithm.
DECLARE Word : STRING
DECLARE V1 : INTEGER
DECLARE V2 : INTEGER
DECLARE Number : INTEGER
DECLARE Index : INTEGER
DECLARE L1 : CHAR
DECLARE L2 : CHAR
DECLARE Continue : BOOLEAN
INPUT Number
1 TO Number
FOR Index
INPUT Word
TRUE
Continue
1
V1
LENGTH(Word)
V2
WHILE Continue = TRUE AND V1 < V2 DO
SUBSTRING(Word, V1, 1)
L1
SUBSTRING(Word, V2, 1)
L2
IF L1 <> L2
THEN
FALSE
Continue
ELSE
V1 + 1
V1
V2 – 1
V2
ENDIF
ENDWHILE
IF Continue = TRUE
THEN
OUTPUT "Successful"
ELSE
OUTPUT "NOT successful"
ENDIF
NEXT Index
(a) Complete the trace table for the algorithm, using the input data:
2, RACECAR, TREAT
Number
Index
Word
Continue
V1
V2
L1
L2
OUTPUT
[6]
(b) State the purpose of the algorithm on page 6.
…
… [1]
(c) Explain how the algorithm knows when all the letters in the word have been checked so that it
can stop checking.
…
…
…
…
…
… [3]
Question text
A database table, CapitalCity, stores data about capital cities from around the world.
9
Field
Description
Identification
a unique identifier
Name
the name of the city
Country
the name of the country in which the city is located
Continent
the name of the continent in which the city is located
Population
the number of people living in the city
Founded
the year the city was founded
AverageIncome
average income of the city’s population, in $, rounded to 2 decimal places
Subway
whether or not the city has an underground railway system
(a) Complete the table by writing one appropriate field from CapitalCity for each data type.
Each field must be different.
Data type
Field
Boolean
integer
real
text
[2]
(b) Complete the structured query language (SQL) statement to list only the name, country and
population of all the cities in CapitalCity from the continent of Asia.
SELECT …
… CapitalCity
WHERE … = … ;
[4]
Due to an issue with Question 10, the question has been removed from the question paper.
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
2
Tick (✓) one box to complete this sentence.
A pseudocode example of a selection statement is
A
CALL Sorting(Value1, Value2)
B
DECLARE Count : INTEGER
C
IF X = 7 THEN Y
21 ENDIF
D
WHILE X <> -1 DO
[1]
Question text
3
Four flowchart symbols and five purposes are shown.
(a) Draw one line to link each flowchart symbol to its correct purpose.
Not all purposes will be used.
Flowchart symbol
Purpose
subroutine
process
flow
decision
terminator
[4]
ĬÍĊ®Ġ´íÈõÏĪÅĊÞû·þ×
,
,
(b) An algorithm needs to total 50 numbers between 1 and 100 inclusive.
Draw a flowchart that:
•
uses a count-controlled loop from 1 to 50
•
uses an appropriate prompt to ask for a number between 1 and 100
•
totals the numbers as they are entered
•
outputs the total after the loop has completed with an appropriate message.
[6]
ĬÏĊ®Ġ´íÈõÏĪÅĊÞù·þ×
Question text
4
This pseudocode algorithm is intended to sort a pre-populated one-dimensional (1D) array named
ItemList into alphabetical order using a bubble sort.
01
DECLARE ItemList : ARRAY[1:100] OF STRING
02
DECLARE Counter : STRING
03
DECLARE Limit : INTEGER
04
DECLARE Pass : INTEGER
05
DECLARE Swapped : BOOLEAN
06
DECLARE Temp : STRING
100
07
Limit
1
08
Pass
TRUE
09
Temp
10
WHILE Swapped = TRUE OR Pass <= Limit - 1 DO
FALSE
11
Swapped
1 TO Limit – Pass
12
FOR Counter
13
IF ItemList[Counter] > ItemList[Counter + 1]
14
THEN
ItemList[Counter]
15
Temp
ItemList[Counter + 1]
16
ItemList[Counter]
Temp
17
ItemList[Counter]
TRUE
18
Swapped
19
ENDCASE
Pass + 1
20
Pass
21
NEXT Counter
22
ENDWHILE
(a) Identify the line numbers of five errors in the pseudocode and suggest a correction for each
error.
Error 1 line number …
Correction …
…
Error 2 line number …
Correction …
…
Error 3 line number …
Correction …
…
Error 4 line number …
Correction …
…
ĬÍĊ®Ġ´íÈõÏĪÅĊàû·Ā×
,
,
Error 5 line number …
Correction …
…
[5]
(b) A bubble sort algorithm can be written to include features that make it more efficient.
Explain why the corrected bubble sort algorithm is efficient.
…
…
…
…
…
… [3]
Question text
7
This pseudocode represents an algorithm.
An input of –1 will terminate the algorithm.
DECLARE Count : INTEGER
DECLARE Answer : INTEGER
DECLARE Value : INTEGER
REPEAT
INPUT Value
IF Value <> -1
THEN
Value
Answer
Value - 1 TO 1 STEP -1
FOR Count
Answer * Count
Answer
NEXT Count
OUTPUT Answer
ENDIF
UNTIL Value = -1
(a) Complete the trace table for the input data:
5, 6, –1, 20, 9, 4
Value
Count
Answer
OUTPUT
[5]
ĬÑĊ®Ġ´íÈõÏĪÅĊÝù¶þ×
,
,
(b) State the purpose of this algorithm.
…
… [1]
(c) Describe the problem that would be caused in this algorithm if a Value of 1, 0 or less than –1
was input.
…
…
…
… [2]
Question text
10 The function LENGTH(X) calculates the length of a string X
Write the pseudocode statements to:
•
allow a line of text to be input to an appropriate variable
•
store this line of text in a text file called Main.txt
•
calculate the length of the line of text and output the text in lower case along with its length
•
store the lower-case line of text in a text file called Lowercase.txt
Make sure that any variables used are declared and that both text files are closed after they have
been used.
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
ĬÓĊ®Ġ´íÈõÏĪÅĊßû¶Ā×