Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 2(iii)(a)

2 marks

Computer ScienceISC 2026 Computer Science Paper 1OutputRecursion

What is the output of the following code?

What will the function workOut(220) return?

Show the code
The following function workOut( ) is a part of some class. Assume 'n' is a positive integer.
String workOut (int n)
{
    if (n == 0)
        return "";
    int rem = n % 16;
    char cr = (rem < 10) ? (char)(rem + '0') : (char)(rem - 10 + 'A');
    return workOut (n / 16) + cr;
}
Open in the Recursion visualiser

Answer

Answer

Dry run of workOut(220): Call 1: workOut(220) -> rem = 220 % 16 = 12 -> cr = (char)(12 - 10 + 'A') = 'C'; calls workOut(13) + 'C' Call 2: workOut(13) -> rem = 13 % 16 = 13 -> cr = (char)(13 - 10 + 'A') = 'D'; calls workOut(0) + 'D' Call 3: workOut(0) returns "" Unwinding: Call 2 returns "" + 'D' = "D" Call 1 returns "D" + 'C' = "DC" Return value: "DC"