Filters
Year Range
20212025
2021
2022
2024
2025
Difficulty
Session
Variant
Sub-topic
15 questions
CAIEA-Level9618-a · Paper 4

Recursion

15 questions· page 1 of 2

Q12021 Oct/Nov·P414 partsMedium-Easy
(a)

Write program code to declare the function Unknown().

Save your program as question 1.

Copy and paste the program code into part 1(a) in the evidence document.

(b)(ii)

Take a screenshot to show the output from part (b)(i).

Copy and paste the screenshot into part 1(b)(ii) in the evidence document.

(c)

Rewrite the function Unknown() as an iterative function, IterativeUnknown().

Save your program.

Copy and paste the program code into part 1(c) in the evidence document.

(d)(ii)

Take one or more screenshots to show the output of both functions for each set of parameters.

Copy and paste the screenshot(s) into part 1(d)(ii) in the evidence document.

Similar questions
Q12021 Oct/Nov·P424 partsMedium-Easy
(a)

Write program code to declare the function Unknown().

Save your program as question 1.

Copy and paste the program code into part 1(a) in the evidence document.

(b)(ii)

Take a screenshot to show the output from part (b)(i).

Copy and paste the screenshot into part 1(b)(ii) in the evidence document.

(c)

Rewrite the function Unknown() as an iterative function, IterativeUnknown().

Save your program.

Copy and paste the program code into part 1(c) in the evidence document.

(d)(ii)

Take one or more screenshots to show the output of both functions for each set of parameters.

Copy and paste the screenshot(s) into part 1(d)(ii) in the evidence document.

Similar questions
Q32022 Oct/Nov·P413 partsMedium
(c)

The following recursive pseudocode function searches the binary tree for a given value. If the value is found, the function must return the index of the value. If the value is not found, the function must return –1.

The function is incomplete. There are four incomplete statements.

FUNCTION SearchValue(Root : INTEGER, 
                     ValueToFind : INTEGER) RETURNS INTEGER
    IF Root = –1 THEN
        RETURN –1
    ELSE
        IF ArrayNodes[Root, 1] = ValueToFind THEN
            RETURN .......................................
        ELSE
            IF ArrayNodes[Root, 1] = –1 THEN
                RETURN –1
            ENDIF
        ENDIF
    ENDIF
    IF ArrayNodes[Root, 1] ....................................... ValueToFind THEN
        RETURN SearchValue(ArrayNodes[............, 0], ValueToFind)
    ENDIF
    IF ArrayNodes[Root, ............] < ValueToFind THEN
        RETURN SearchValue(ArrayNodes[Root, 2], ValueToFind)
    ENDIF
ENDFUNCTION

Write program code for the function SearchValue().

Save your program.

Copy and paste the program code into part 3(c) in the evidence document.

(d)

A post order traversal performs the following operation:

  • visit the left node
  • visit the right node
  • output the root.

For example, in the following tree, the output would be: 3 9 25 60 50

An outline of the PostOrder() procedure is:

  • If left node is not empty, make a recursive call with the left node as the root.
  • If right node is not empty, make a recursive call with the right node as the root.
  • Output the current root node.

The procedure PostOrder() takes the root node as a parameter.

Write program code for the procedure PostOrder().

Save your program.

Copy and paste the program code into part 3(d) in the evidence document.

(e)(ii)

Test your program.

Take a screenshot to show the output.

Copy and paste the screenshot into part 3(e)(ii) in the evidence document.

Similar questions
Q32022 Oct/Nov·P433 partsMedium
(c)

The following recursive pseudocode function searches the binary tree for a given value. If the value is found, the function must return the index of the value. If the value is not found, the function must return –1.

The function is incomplete. There are four incomplete statements.

FUNCTION SearchValue(Root : INTEGER, 
                     ValueToFind : INTEGER) RETURNS INTEGER
   IF Root = –1 THEN
      RETURN –1
   ELSE
      IF ArrayNodes[Root, 1] = ValueToFind THEN
         RETURN .......................................
      ELSE
         IF ArrayNodes[Root, 1] = –1 THEN
            RETURN –1
         ENDIF
      ENDIF
   ENDIF
   IF ArrayNodes[Root, 1] ....................................... ValueToFind THEN
      RETURN SearchValue(ArrayNodes[............, 0], ValueToFind)
   ENDIF
   IF ArrayNodes[Root, ............] < ValueToFind THEN
      RETURN SearchValue(ArrayNodes[Root, 2], ValueToFind)
   ENDIF
ENDFUNCTION

Write program code for the function SearchValue().

Save your program.

Copy and paste the program code into part 3(c) in the evidence document.

(d)

A post order traversal performs the following operation:

  • visit the left node
  • visit the right node
  • output the root.

For example, in the following tree, the output would be: 3 9 25 60 50

An outline of the PostOrder() procedure is:

  • If left node is not empty, make a recursive call with the left node as the root.
  • If right node is not empty, make a recursive call with the right node as the root.
  • Output the current root node.

The procedure PostOrder() takes the root node as a parameter.

Write program code for the procedure PostOrder().

Save your program.

Copy and paste the program code into part 3(d) in the evidence document.

(e)(ii)

Test your program.

Take a screenshot to show the output.

Copy and paste the screenshot into part 3(e)(ii) in the evidence document.

Similar questions
Q12023 Oct/Nov·P413 partsMedium-Hard
(b)(i)

Rewrite the function IterativeVowels() as a recursive function with the identifier RecursiveVowels().

Save your program.

Copy and paste the program code into part 1(b)(i) in the evidence document.

(b)(ii)

Write program code to call the function RecursiveVowels() with the parameter "imagine" from the main program.

Output the return value.

Save your program.

Copy and paste the program code into part 1(b)(ii) in the evidence document.

(b)(iii)

Test your program.

Take a screenshot of the output.

Save your program.

Copy and paste the screenshot into part 1(b)(iii) in the evidence document.

Similar questions
Q12023 Oct/Nov·P433 partsMedium-Hard
(b)(i)

Rewrite the function IterativeVowels() as a recursive function with the identifier RecursiveVowels().

Save your program.

Copy and paste the program code into part 1(b)(i) in the evidence document.

(b)(ii)

Write program code to call the function RecursiveVowels() with the parameter "imagine" from the main program.

Output the return value.

Save your program.

Copy and paste the program code into part 1(b)(ii) in the evidence document.

(b)(iii)

Test your program.

Take a screenshot of the output.

Save your program.

Copy and paste the screenshot into part 1(b)(iii) in the evidence document.

Similar questions
Q22023 Oct/Nov·P422 partsMedium-Hard
(b)(i)

Write program code for RecursiveValue().

Save your program.

Copy and paste the program code into part 2(b)(i) in the evidence document.

(b)(iii)

Test your program.

Take a screenshot of the output.

Save your program.

Copy and paste the screenshot into part 2(b)(iii) in the evidence document.

Similar questions
Q32024 May/Jun·P422 partsMedium-Hard
(b)(i)

The following recursive pseudocode function sorts the array into ascending order using an insertion sort and returns the sorted array.

DECLARE LastItem : INTEGER
DECLARE CheckItem : INTEGER
DECLARE LoopAgain : BOOLEAN
FUNCTION RecursiveInsertion(IntegerArray : ARRAY[] OF INTEGER, 
                            NumberElements : INTEGER) RETURNS ARRAY[] OF INTEGER
   IF NumberElements <= 1 THEN
      RETURN IntegerArray
   ELSE
      CALL RecursiveInsertion(IntegerArray, NumberElements - 1)
      LastItem ← IntegerArray[NumberElements - 1]
      CheckItem ← NumberElements - 2
   ENDIF
   LoopAgain ← TRUE
   IF CheckItem < 0 THEN
      LoopAgain ← FALSE
   ELSE 
      IF IntegerArray[CheckItem] < LastItem THEN
         LoopAgain ← FALSE
      ENDIF
   ENDIF
   WHILE LoopAgain
      IntegerArray[CheckItem + 1] ← IntegerArray[CheckItem]
      CheckItem ← CheckItem - 1
      IF CheckItem < 0 THEN
         LoopAgain ← FALSE
      ELSE 
         IF IntegerArray[CheckItem] < LastItem THEN
            LoopAgain ← FALSE
         ENDIF
      ENDIF
   ENDWHILE
   IntegerArray[CheckItem + 1] ← LastItem
   RETURN IntegerArray
ENDFUNCTION

Write the program code for the pseudocode function RecursiveInsertion().

Save your program.

Copy and paste the program code into part 3(b)(i) in the evidence document.

(d)(i)

Write program code for the recursive function BinarySearch().

Save your program.

Copy and paste the program code into part 3(d)(i) in the evidence document.

Similar questions
Q22022 May/Jun·P422 partsMedium-Hard
(c)(i)

Write program code for the recursive function BinarySearch().

Save your program.

Copy and paste the program code into part 2(c)(i) in the evidence document.

(c)(ii)

In the main program, test the function BinarySearch() twice, outputting the returned value each time.

One test should be for a number that is in the first line of the array.
One test should be for a number that is not in the first line of the array.

Take a screenshot to show the output.

Copy and paste the screenshot into part 2(c)(ii) in the evidence document.

Similar questions
Q22025 Oct/Nov·P423 partsMedium-Hard
(e)

The recursive function RecursiveBinarySearch() takes four parameters:

  • an integer array
  • the lower bound of the array
  • the upper bound of the array
  • the value to find in the array.

The recursive function performs a binary search to find the index of the value in the array.

The function returns the index of the value if it is found. The function returns -1 if the value is not found.

Write program code for RecursiveBinarySearch()

Save your program.

Copy and paste the program code into part 2(e) in the evidence document.

(f)(i)

The main program:

  • prompts the user to enter an integer
  • takes the integer as input
  • calls RecursiveBinarySearch() with the sorted array, appropriate lower bound, appropriate upper bound and the user's input as parameters
  • outputs "Not found" if the input is not within the array
  • outputs "Found at position" and the index if the input is within the array.

Write program code to amend the main program.

Save your program.

Copy and paste the program code into part 2(f)(i) in the evidence document.

(f)(ii)

Test your program three times with each of the inputs described:

Test 1: the smallest number in the array

Test 2: the largest number in the array

Test 3: a number not in the array

Take a screenshot of each output.

Save your program.

Copy and paste the screenshot(s) into part 2(f)(ii) in the evidence document.

Similar questions