Computational thinking: searching, sorting, ADTs and recursion – A Level Computer Science (old syllabus, to 2021) (9608)
11 questions · Clear filters
Question text
2
(a) Programs can be written using recursion.
Tick (✓) one or more boxes to show the features that must be included in a valid recursive
algorithm.
Feature
Must be included
Incrementation
General case
Base case
Selection case
It calls itself
[2]
(b) The following recursive procedure outputs every even number from the positive parameter
value down to and including 2.
The procedure checks if the integer parameter is an even or an odd number. If the number is
odd, the procedure converts it to an even number by subtracting 1 from it.
The function MOD(ThisNum : INTEGER, ThisDiv : INTEGER) returns the remainder
value when ThisNum is divided by ThisDiv.
Complete the pseudocode for the recursive procedure.
PROCEDURE Count(BYVALUE … : INTEGER)
IF … (Number, 2) <> 0
THEN
Number – 1
Number
ENDIF
OUTPUT …
IF Number > 0
THEN
… ( … – 1)
ENDIF
ENDPROCEDURE
[5]
(c) A program allows guests to input a meal option at a wedding.
Guests can choose meal option 1 or meal option 2.
The program will keep count of the numbers of each meal option chosen.
The program ends when a value other than 1 or 2 is entered. It then outputs the count of each
meal option.
PROCEDURE MealsCount(BYREF MealOption1 : INTEGER, MealOption2 : INTEGER)
DECLARE MealOption : INTEGER
DECLARE MoreMeals : BOOLEAN
True
MoreMeals
WHILE MoreMeals = True
INPUT MealOption
IF MealOption = 1
THEN
MealOption1 + 1
MealOption1
ELSE
IF MealOption = 2
THEN
MealOption2 + 1
MealOption2
ELSE
OUTPUT MealOption1, " ", MealOption2
False
MoreMeals
ENDIF
ENDIF
ENDWHILE
ENDPROCEDURE
The program contains a conditional loop.
Use pseudocode to rewrite the conditional loop as a recursive algorithm.
PROCEDURE MealsCount(BYREF MealOption1 : INTEGER, MealOption2 : INTEGER)
DECLARE MealOption : INTEGER
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
3
Recursive algorithms can be used when creating programs.
(a) Describe what is meant by a recursive algorithm.
…
…
…
…
…
… [3]
(b) A string that is a palindrome, reads the same forwards as it does backwards. For example,
the name Anna is a palindrome.
The function Substring(Variable, StartingCharacter, NumberOfCharacters)
returns one or more characters from a string. The first character is at position 0.
For example, the string "Happy" is stored in the variable Word.
•
Substring(Word, 1, 1) would return the character "a".
•
Substring(Word, 2, 3) would return the characters "ppy".
The function Length() returns the length of the string as an integer. For example,
Length(Word) returns 5.
The following is a recursive function to find out whether a string is a palindrome. The function
returns True if the parameter is a palindrome, and returns False if it is not a palindrome.
Complete the pseudocode for the recursive algorithm to indicate whether a string is a
palindrome.
FUNCTION IsPalindrome(CheckWord : STRING) RETURNS BOOLEAN
IF … <= 1
THEN
RETURN …
ENDIF
IF Substring(CheckWord, 0, 1) <>
Substring(CheckWord, … (CheckWord)-1, 1)
THEN
RETURN …
ELSE
RETURN … ( Substring(CheckWord, 1,
Length(CheckWord)-2))
ENDIF
ENDFUNCTION
[5]
(c) The function FindPower() is a recursive function that calculates the result of a base number
to the power of the exponent. For example, the result of 2 4 = 16, as 2*2*2*2 = 16. In this
example, 2 is the base number and 4 is the exponent.
The base number and the exponent are passed as parameters.
Write pseudocode for the recursive function FindPower(). Assume both the base and the
exponent are positive integers.
…
…
…
…
…
…
…
…
…
Question text
2
A computer games club wants to run a competition. The club needs a system to store the scores
achieved in the competition.
A selection of score data is as follows:
99, 125, 121, 97, 109, 95, 135, 149
(a) A linked list of nodes will be used to store the data. Each node consists of the data, a left
pointer and a right pointer. The linked list will be organised as a binary tree.
(i)
Complete the binary tree to show how the score data above will be organised.
RootPointer
The symbol ∅ represents a null pointer.
LeftPointer
RightPointer
99
97
125
∅
121
[5]
(ii)
The following diagram shows a 2D array that stores the nodes of the binary tree’s linked
list.
Add the correct pointer values to complete the diagram, using your answer from
part (a)(i).
RootPointer
Index LeftPointer
Data
RightPointer
0
99
0
1
125
2
121
3
97
FreePointer
4
109
5
95
6
135
7
149
8
[6]
(b) The club also considers storing the data in the order in which it receives the scores as a
linked list in a 1D array of records.
The following pseudocode algorithm searches for an element in the linked list.
Complete the six missing sections in the algorithm.
FUNCTION FindElement(Item : INTEGER) RETURNS …
… ← RootPointer
WHILE CurrentPointer … NullPointer
IF List[CurrentPointer].Data <> …
THEN
CurrentPointer ← List[ … ].Pointer
ELSE
RETURN CurrentPointer
ENDIF
ENDWHILE
CurrentPointer ← NullPointer
… CurrentPointer
ENDFUNCTION
[6]
(c) The games club is looking at two programming paradigms: imperative and object-oriented
programming paradigms.
Describe what is meant by the imperative programming paradigm and the object-oriented
programming paradigm.
(i)
Imperative …
…
…
…
…
… [3]
(ii)
Object-oriented …
…
…
…
…
… [3]
(d) Players complete one game to place them into a category for the competition. The games club
wants to implement a program to place players into the correct category. The programmer
has decided to use object-oriented programming (OOP).
The highest score that can be achieved in the game is 150. Any score less than 50 will not
qualify for the competition. Players will be placed in a category based on their score.
The following diagram shows the design for the class Player. This includes the properties
and methods.
Player
Sco
Question text
3
Some algorithms can be written using recursion.
(a) State two features of recursion.
Feature 1 …
Feature 2 …
[2]
(b) Explain what a compiler has to do to implement recursion.
…
…
…
…
…
… [3]
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 the Cambridge Assessment Group. Cambridge Assessment is the brand name of the University of
Question text
1
A company wants an online marking system for an examination.
(a) The following is a selection of data showing final marks.
36, 45, 21, 65, 66, 13, 54, 53, 34
A linked list of nodes will be used to store the data. Each node consists of the data, a left
pointer and a right pointer. The linked list will be organised as a binary tree.
(i)
Complete the binary tree to show how the data above will be organised.
RootPointer
The symbol Ø represents a
null pointer
LeftPointer
RightPointer
36
45
21
Ø
65
66
Ø
Ø
[5]
(ii)
The following diagram shows a 2D array that stores the nodes of the binary tree’s linked
list.
Add the correct pointer values to complete the diagram, using your answer from
part (a)(i).
RootPointer
Index
LeftPointer
Data
RightPointer
0
0
36
1
45
FreePointer
2
21
3
65
4
66
5
13
6
54
7
53
8
34
9
[6]
(b) The company wants to implement a program for the marking system. It will do this with
object-oriented programming (OOP).
Many candidates take the examination. Each examination paper is given a PaperID that is
made up of the centre (school) number followed by the candidate number.
Each examination paper is awarded a grade.
The following diagram shows the design for the ExaminationPaper class. This includes
the attributes and methods.
ExaminationPaper
FinalMark : INTEGER // maximum 2 digits, initialised to 0
Grade
: STRING // "Pass", "Merit", "Distinction"
// or "Fail", initialised to "Fail"
PaperID
: STRING // centre number followed by the
// candidate number, for example
// "ZZ00991001"
Create()
//
creates and initialises a new instance
//
of the ExaminationPaper class using
//
language-appropriate constructor
SetFinalMark()
//
checks that the mark parameter has a
//
valid value, if so, assigns it to
//
FinalMark
SetGrade()
//
sets Grade based on FinalMark
GetFinalMark()
//
returns FinalMark
GetGrade()
//
returns Grade
GetPaperID()
//
returns PaperID
(i)
The constructor receives the centre number and candidate number as parameter values
to create PaperID. Other properties are initialised as instructed in the class diagram.
Write program code for the Create() constructor method.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
… [5]
(ii)
Get and set methods are used to support the security and integrity of data in
object-oriented programming.
Explain how get and set methods are used to support security and integrity.
…
Question text
4
(a) A program has sorted some data in the array, List, in ascending order.
The following binary search algorithm is used to search for a value in the array.
FALSE
01
ValueFound
LengthOfList - 1
02
UpperBound
0
03
LowerBound
FALSE
04
NotInList
05
06
WHILE ValueFound = FALSE AND NotInList = FALSE
ROUND((LowerBound + UpperBound) / 2)
07
MidPoint
08
09
IF List[LowerBound] = SearchValue
10
THEN
TRUE
11
ValueFound
12
ELSE
13
IF List[MidPoint] < SearchValue
14
THEN
MidPoint + 1
15
UpperBound
16
ELSE
MidPoint – 1
17
UpperBound
18
ENDIF
19
IF LowerBound > MidPoint
20
THEN
TRUE
21
NotInList
22
ENDIF
23
ENDIF
24
ENDWHILE
25
26
IF ValueFound = FALSE
27
THEN
28
OUTPUT "The value is in the list"
29
ELSE
30
OUTPUT "The value is not found in the list"
31
ENDIF
Note:
The pseudocode function
ROUND(Reall : REAL) RETURNS INTEGER
rounds a number to the nearest integer value.
For example: ROUND(4.5) returns 5 and ROUND(4.4) returns 4
(i)
There are four errors in the algorithm.
Write the line of code where an error is present and write the correction in pseudocode.
Error 1 …
Correction …
Error 2 …
Correction …
Error 3 …
Correction …
Error 4 …
Correction …
[4]
(ii)
A binary search is one algorithm that can be used to search an array.
Identify another searching algorithm.
… [1]
(b) The following is an example of a sorting algorithm. It sorts the data in the array ArrayData.
""
01
TempValue
02
REPEAT
TRUE
03
Sorted
0 TO 4
04
FOR Count
05
IF ArrayData[Count] > ArrayData[Count + 1]
06
THEN
ArrayData[Count + 1]
07
TempValue
ArrayData[Count]
08
ArrayData[Count + 1]
TempValue
09
ArrayData[Count]
FALSE
10
Sorted
11
ENDIF
12
ENDFOR
13
UNTIL Sorted = TRUE
(i)
Complete the trace table for the algorithm given in part (b), for the ArrayData values
given in the table.
ArrayData
Count
TempValue
Sorted
0
1
2
3
4
5
5
20
12
25
32
29
[4]
(ii)
Rewrite lines 4 to 12 of the algorithm in part (b) using a WHILE loop instead of a FOR
loop.
…
…
…
…
…
…
…
Question text
2
The array ItemList[1:20] stores data. A bubble sort sorts these data.
(a) Complete the pseudocode algorithm for a bubble sort.
20
01
MaxIndex
…
02
NumberItems
1 TO …
03
FOR Outer
1 to NumberItems
04
FOR Inner
IF ItemList[Inner] > …
05
06
THEN
ItemList[ … ]
07
Temp
ItemList[ … ]
08
ItemList[Inner]
…
09
ItemList[Inner + 1]
10
ENDIF
11
ENDFOR
…
12
NumberItems
13
ENDFOR
[7]
(b) The algorithm in part (a) is inefficient.
(i)
Explain why the algorithm in part (a) is inefficient.
…
…
…
… [2]
(ii)
Explain how you would improve the efficiency of this algorithm.
…
…
…
…
… [3]
(c) An insertion sort is another sorting algorithm.
State two situations when an insertion sort is more efficient than a bubble sort. Give a reason
for each.
Situation 1 …
…
Reason …
…
…
Situation 2 …
…
Reason …
…
…
[4]
96
Question text
3
The arrays PollData[1:10] and CardData[1:10] store data.
PollData
12
85
52
57
25
11
33
59
56
91
CardData
11
12
25
33
52
56
57
59
91
85
An insertion sort sorts these data.
(a) State why it will take less time to complete an insertion sort on CardData than on PollData.
…
… [1]
(b) The following pseudocode algorithm performs an insertion sort on the CardData array.
Complete the following pseudocode algorithm.
01
ArraySize ← 10
FOR Pointer ← 2 TO …
02
03
ValueToInsert ← CardData[Pointer]
HolePosition ← …
04
WHILE (HolePosition > 1 AND ( … > … ))
05
CardData[HolePosition] ← CardData[ … ]
06
HolePosition ← …
07
08
ENDWHILE
CardData[HolePosition] ← …
09
10
ENDFOR
[7]
(c) (i)
A binary search algorithm is used to find a specific value in an array.
Explain why an array needs to be sorted before a binary search algorithm can be used.
…
…
…
…
…
…
…
… [2]
(ii)
The current contents of CardData are shown.
11
12
25
33
52
56
57
59
85
91
Explain how a binary search will find the value 25 in CardData.
…
…
…
…
…
…
…
…
Question text
6
An Abstract Data Type (ADT) is used to create an unordered binary tree. The binary tree is created
as an array of nodes. Each node consists of a data value and two pointers.
A record type, Node, is declared using pseudocode.
TYPE Node
DECLARE DataValue : STRING
DECLARE LeftPointer : INTEGER
DECLARE RightPointer : INTEGER
ENDTYPE
The following statement declares an array BinaryTree.
DECLARE BinaryTree : ARRAY[0:14] OF Node
A variable, NextNode, points to the next free node.
The following diagram shows a possible node.
ArrayIndex
DataValue
LeftPointer
RightPointer
0
"Red"
1
2
The commands in the following table create and add nodes to the binary tree.
Command
Comment
CreateTree(NodeData)
Sets NextNode to 0.
Writes NodeData into DataValue at the position NextNode
Updates NextNode using NextNode = NextNode + 1
AttachLeft(NodeData, ParentNode)
Writes NodeData into DataValue of NextNode
Sets the LeftPointer of node ParentNode to NextNode
Updates NextNode using NextNode = NextNode + 1
AttachRight(NodeData, ParentNode)
Writes NodeData into DataValue of NextNode
Sets the RightPointer of node ParentNode to NextNode
Updates NextNode using NextNode = NextNode + 1
(a) The following commands are executed.
CreateTree("Red")
AttachLeft("Blue", 0)
AttachRight("Green", 0)
The following diagram shows the current state of the binary tree.
0
"Red"
1
2
1
"Blue"
2 "Green"
…
…
…
…
…
…
…
…
…
…
…
…
Write on the diagram to show the state of the binary tree after the following commands have
been executed.
AttachRight("Black", 2)
AttachLeft("Brown", 2)
AttachLeft("Peach", 3)
AttachLeft("Yellow", 1)
AttachRight("Purple", 1)
AttachLeft("White", 6)
AttachLeft("Pink", 7)
AttachLeft("Grey", 9)
AttachRight("Orange", 9)
[5]
(b) A new command has been added to initialise the pointers of the binary tree to − 1 to indicate
they are not in use.
A leaf is a node of the binary tree which has no children. In the case of this binary tree, a node
with a LeftPointer of − 1 and a RightPointer of − 1 is a leaf.
Write a recursive function, in program code, to traverse the binary tree and output the value
of DataValue for each leaf node.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…
Question text
3
NameList is a 1D array that stores a sorted list of names. A programmer declares the array in
pseudocode as follows:
NameList : Array[0 : 100] OF STRING
The programmer wants to search the list using a binary search algorithm.
The programmer decides to write the search algorithm as a recursive function. The function, Find,
takes three parameters:
•
Name, the string to be searched for
•
Start, the index of the first item in the list to be searched
•
Finish, the index of the last item in the list to be searched
The function will return the position of the name in the list, or −1 if the name is not found.
Complete the pseudocode for the recursive function.
FUNCTION Find(BYVALUE Name : STRING, BYVALUE Start : INTEGER,
BYVALUE Finish : INTEGER) RETURNS INTEGER
// base case
IF …
THEN
RETURN −1
ELSE
…
Middle
IF …
THEN
RETURN …
ELSE
// general case
IF SearchItem > …
THEN
…
ELSE
…
ENDIF
ENDIF
ENDIF
[7]
ENDFUNCTION
Question text
2
(a) (i)
State what is meant by a recursively defined procedure.
…
… [1]
(ii)
Write the line number from the pseudocode shown in part (b) that shows the
procedure X is recursive. …
[1]
(b) The recursive procedure X is defined as follows:
01 PROCEDURE X(Index, Item)
02
IF MyList[Index] > 0
03
THEN
04
IF MyList(Index) >= Item
05
THEN
06
MyList[Index] ← MyList[Index + 1]
07
ENDIF
08
CALL X(Index + 1, Item)
09
ENDIF
10 ENDPROCEDURE
An array MyList is used to store a sorted data set of non-zero integers. Unused cells contain
zero.
1
2
3
4
5
6
7
8
9
10
MyList
3
5
8
9
13
16
27
0
0
0
(i)
Complete the trace table for the dry-run of the pseudocode for the procedure
CALL X(1, 9).
MyList
Index
Item
1
2
3
4
5
6
7
8
9
10
3
5
8
9
13
16
27
0
0
0
1
9
[4]
State the purpose of procedure X when used with the array MyList.
(ii)
…
… [1]