Date | May 2018 | Marks available | 3 | Reference code | 18M.2.HL.TZ0.19 |
Level | HL | Paper | 2 | Time zone | no time zone |
Command term | Explain | Question number | 19 | Adapted from | N/A |
Question
Consider the following recursive method.
public void recursionEx(int x)
{
if(x != 0)
{
System.out.println(x);
recursionEx(x - 1);
System.out.println(x);
}
}
Identify two essential features of a recursive algorithm.
Copy and complete the following table to show the output when the method is called by: recursionEx(3)
;
Explain what would happen if the method was called by recursionEx(-3)
.
Identify three features which should be included in either program code or UML diagrams to help programmers understand and modify programs in the future.
Markscheme
The method calls itself;
With a changing parameter;
There is a base (terminating) case;
Award marks as follows:
Outputting 6 values;
Correct parameter list
First 3 output numbers correct;
Last 3 output numbers correct;
An infinite loop / program will never end/terminate;
Will eventually cause the program to crash;
As it runs out of memory for the parameters stored / Stack overflow;
As the base case is never reached;
Comments can be added;
Use of meaningful names;
Correct indentation;
Relationships/dependencies shown between class (UML) diagrams;