Skip to content
ExamSpine

Lesson 4.7 · Answer it

Scope shadow

2 marksYou mark it yourself against the marking points

Look at this code. The first total is made outside any function, so every function can use it: it is a global variable (in Java, a static variable written in the class outside every method; Java calls it a static field).

total = 10

def reset():
    total = 0
    print(total)

reset()
print(total)
static int total = 10;

static void reset() {
    int total = 0;
    System.out.println(total);
}

// in main:
reset();
System.out.println(total);

It prints 0 and then 10. Explain why the second print shows 10, not 0. [2]