Computational thinking: searching, sorting, ADTs and recursion – A Level Computer Science (old syllabus, to 2021) (9608)
22 questions · page 1 of 2
Question text
5
The recursive algorithm for the Recursion() function is defined in pseudocode as follows:
FUNCTION Recursion(A, B : INTEGER) RETURNS INTEGER
IF A <= 100
THEN
RETURN 1
ELSE
IF A > B
THEN
RETURN 5 + Recursion(A - 1, B)
ELSE
RETURN 10 + Recursion(A – 10, B)
ENDIF
ENDIF
ENDFUNCTION
(a) The function is called with the following pseudocode statement:
OUTPUT Recursion(104, 102)
Dry run the function and complete the trace table. Give the output the program will produce.
Trace table:
A
B
Function call
Return value
Output = …
Working …
…
…
…
…
…
[4]
(b) Rewrite the function Recursion() in pseudocode, using an iterative algorithm.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [4]
Question text
8
Data entered into a computer is stored in an ordered binary tree.
The binary tree is stored in a 2D array, BinaryTree.
The first element of the array is index 0.
(a) The current contents of the binary tree are:
50
35
67
2
43
52
77
Complete the LeftPointer and RightPointer values in the following table for the binary
tree shown.
A null pointer is represented by –1.
RootNode
Index
LeftPointer
Data
RightPointer
0
[0]
50
[1]
67
[2]
77
[3]
35
[4]
2
[5]
43
[6]
52
[7]
[8]
[9]
[10]
[2]
(b) A post-order tree traversal outputs the left node, then the right node, then the root node.
In the tree given in part (a), the post-order tree traversal would output:
2
43
35
52
77
67
50
Complete the following recursive pseudocode algorithm PostOrder().
PROCEDURE PostOrder( … : INTEGER)
IF BinaryTree[RootNode, 0] <> -1
THEN
… (BinaryTree[RootNode, … ])
ENDIF
IF BinaryTree[RootNode, 2] <> -1
THEN
… (BinaryTree[RootNode, 2])
ENDIF
OUTPUT BinaryTree[RootNode, … ]
ENDPROCEDURE
[5]
Question text
1
An array, NumberArray, stores 100 integer values. The array needs to be sorted into ascending
numerical order.
(a) Describe how an insertion sort will sort the data in NumberArray.
…
…
…
…
…
…
…
…
…
… [4]
(b) Another type of sorting algorithm is a bubble sort.
The procedure Bubble() takes an array as a parameter. It performs a bubble sort on the
array. The sorting algorithm stops as soon as all the elements are in ascending order.
Complete the procedure Bubble().
PROCEDURE Bubble(BYREF NumberArray : ARRAY[0 : 99] OF INTEGER)
DECLARE Outer : INTEGER
DECLARE Swap : BOOLEAN
DECLARE Inner : INTEGER
DECLARE Temp : INTEGER
LENGTH(NumberArray) - 1
Outer
REPEAT
…
Inner
FALSE
Swap
REPEAT
IF NumberArray[Inner] > NumberArray[Inner + 1]
THEN
NumberArray[Inner]
Temp
NumberArray[Inner + 1]
NumberArray[Inner]
Temp
NumberArray[Inner + 1]
…
Swap
ENDIF
Inner + 1
Inner
UNTIL Inner = …
Outer - 1
Outer
UNTIL Swap = … OR Outer = …
ENDPROCEDURE
[5]
Question text
4
Each node of a binary tree is a record. Each record has a left pointer, an integer data value
between 0 and 100 inclusive, and a right pointer.
For example:
Item
Example data
LeftPointer
2
Data
34
RightPointer
3
(a) Write pseudocode to declare the record with the identifier Node.
…
…
…
…
…
…
… [2]
(b) Write pseudocode to declare a new node, Node100, and assign 100 to its data value, 1 to
the left pointer and 4 to the right pointer.
…
…
…
…
…
…
… [3]
(c) The ordered binary tree is stored as a 1D global array named BinaryTree of type Node.
RootNode and FreePointer are declared as global variables.
A null pointer is represented by –1.
The current state of the binary tree is shown in the following table:
RootNode
Index
LeftPointer
Data
RightPointer
0
[0]
1
23
3
FreePointer
6
[1]
-1
5
2
[2]
-1
8
4
[3]
5
100
-1
[4]
-1
9
-1
[5]
-1
88
-1
[6]
-1
null
-1
[7]
-1
null
-1
(i)
State the purpose of the free pointer.
…
… [1]
(ii)
Identify an appropriate integer value to represent null data.
… [1]
(iii)
Draw the current state of the binary tree.
[2]
(iv)
The procedure AddData():
•
takes the node to be added to the tree as a parameter
•
finds the location for the node to be stored
•
stores the node in the next free array index
•
stores –1 in the new node’s LeftPointer and RightPointer
•
updates the pointers in the other nodes
•
updates FreePointer.
Complete the pseudocode for the procedure AddData().
PROCEDURE AddData(NewNode)
…
BinaryTree[FreePointer]
BinaryTree[FreePoi
Question text
5
Study the following recursive pseudocode algorithm.
FUNCTION Recursive(Num1, Num2 : INTEGER) RETURNS INTEGER
IF Num1 > Num2
THEN
RETURN 10
ELSE
IF Num1 = Num2
THEN
RETURN Num1
ELSE
RETURN Num1 + Recursive(Num1 * 2, Num2)
ENDIF
ENDIF
ENDFUNCTION
(a) The function is called as follows:
Recursive(1, 15)
Dry run the function and complete the trace table. Give the final return value.
Trace table:
Num1
Num2
Function call
Return value
Final return value …
Working …
…
…
…
…
[4]
(b) Rewrite the function Recursive() in pseudocode, using an iterative algorithm.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
5
There are several sorting algorithms. One type of sorting algorithm is an insertion sort.
(a) Explain how an insertion sort puts a set of data into ascending order.
…
…
…
…
…
…
…
…
…
… [4]
(b) The following algorithm performs a bubble sort. It is currently incomplete.
Complete the algorithm.
NumberOfItems – 2
Counter
REPEAT
FALSE
DataSwapped
0 TO …
FOR CurrentValue
IF DataList[CurrentValue] > DataList[CurrentValue + 1]
THEN
DataList[ … ]
ValueTemp
DataList[CurrentValue + 1]
DataList[CurrentValue]
…
DataList[CurrentValue + 1]
…
DataSwapped
ENDIF
ENDFOR
UNTIL DataSwapped = …
[5]
Question text
12 ENDFUNCTION
(a) State the line number in the pseudocode algorithm that shows function NumberPattern()
is recursive. Justify your choice.
Line number …
Justification …
…
…
[2]
(b) The function is called as follows:
NumberPattern(1,1,12)
Dry run the algorithm and complete the following trace table. State the final value returned.
Show your working.
Value1
Value2
Temp
EndValue
OUTPUT
RETURN value
Final value returned …
Working
…
…
…
…
…
…
…
[5]
(c) State the purpose of the algorithm.
…
… [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 the Cambridge Assessment Group. Cambridge Assessment is the brand name of the University of
Question text
1
There are several different searching and sorting algorithms.
(a) Identify two sorting algorithms.
1 …
2 …
[2]
(b) Consider the following pseudocode algorithm.
0
LowerBound
LengthOfList – 1
UpperBound
FALSE
ValueFound
OUTPUT "Value to find: "
INPUT ValueToFind
WHILE ValueFound = FALSE AND UpperBound <> LowerBound
(LowerBound + UpperBound)DIV 2
MidPoint
IF List[MidPoint] = ValueToFind
THEN
TRUE
ValueFound
ELSE
IF List[MidPoint] < ValueToFind
THEN
MidPoint + 1
LowerBound
ELSE
MidPoint – 1
UpperBound
ENDIF
ENDIF
ENDWHILE
IF ValueFound = FALSE
THEN
(LowerBound + UpperBound) DIV 2
MidPoint
IF List[MidPoint] = ValueToFind
THEN
OUTPUT "Item in position " & MidPoint & " in list"
ELSE
OUTPUT "Not in list"
ENDIF
ELSE
OUTPUT "Item in position " & MidPoint & " in list"
ENDIF
Note: DIV is an operator that performs integer division.
The array List contains the following values:
2, 5, 21, 25, 36, 48, 51, 59, 65, 70
(i)
Complete the trace table to show a dry run of the algorithm, when the value 21 is input.
LowerBound
UpperBound
ValueFound
ValueToFind
MidPoint
[3]
(ii)
Identify this type of searching algorithm.
… [1]
(iii)
The value 59 is input.
State the number of times the while loop condition is executed.
… [1]
(iv)
State the minimum number of times the while loop condition will be executed to search
for a value.
… [1]
(v)
MidPoint is calculated and checked again after the while loop is terminated.
Explain why this additional calculation and check is necessary.
…
…
…
… [2]
(vi)
A new data set is used as follows:
5, 9, 10, 12, 15, 13, 17, 19, 20, 2
Explain why the algorithm will not find the value 2 in this data set.
…
…
…
… [2]
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
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.
…
…
…
…
…
…
…
…
…