Skip to content
ExamSpine

Answer it

P2 b2 leaderboard recursion read

3 marksYou mark it yourself against the marking points

An online puzzle game keeps its players' scores in a list (an array in Java) called scores. This recursive function adds up the first n scores.

def total(scores, n):
    if n == 0:
        return 0
    return scores[n - 1] + total(scores, n - 1)
static int total(int[] scores, int n) {
    if (n == 0) {
        return 0;
    }
    return scores[n - 1]
        + total(scores, n - 1);
}

(a) Identify the base case of the function. [1]

(b) The function is called as total with scores [40, 25, 10, 5] and n = 3. State the value it returns. [1]

(c) State how many times total is called in (b), counting the first call. [1]

[3]