Construct code to implement assessment statements D.3.1–D.3.4.


Teaching Note:

Students may be asked to trace, explain or construct algorithms using the concepts associated with the terms.


Sample Questions - General Tracing Questions from the Former Curriculum, which could apply here, and the next few assessment statements:

Study the algorithm shown below and answer the questions that follow.

public class Prog
{ public static void main(String args [])
    {  new Prog();

    public Prog()
    { String s = "xxx";

    void getData(String p)
    { String a, b;
        a = inputString("Enter String");
        while (!a.equals(p))
        { b = inputString("Enter String");
            a = inputString("Enter String");

    String zing(String a, String b)
    { if (a.length() > 6) return "*******";
        { int d = a.length();
            for (int i=0; i<6-d; i++)
                a = a + " ";
            return (a+b);

The following pairs of data are entered into the program. The first value is stored in
variable a and the second in variable b in method getData().

Value stored in variable a         Value stored in variable b
abc                                defg
abcdef                             g
abcdefg                            h
ab                                 cedfg
(a) Copy and complete the following trace table to show what is output at line
output(zing(a,b)); for each pair of inputs above.
a=inputString();      |       b=inputString();       |      output
abc                   |       ____                   |      ____                
____                  |       defg                   |      ____

[3 marks]

(b) Explain why the trace does not reach the line output("end"); . [2 marks]

(From here on, it's more analysis of algorithms, rather than tracing of algorithms... might not apply here, but might, in the new curriculum.)

(c) Suggest a way to fix this problem. [1 mark] (refer to the sample question in 2.1.4, and accompanying questions (a) and (b)).

(e) Outline what method zing() does and suggest a practical use for the methodwhen outputting a table of data values. [2 marks]
(also refer to the sample question in 2.1.4, and accompanying questions (a) and (b)).

JSR Notes:

The best thing here is to look at lots of past papers.