The big idea: A variable is a name for a place in memory holding one value.
Its data type says what kind of value that is — which decides both what can be stored and what can be done with it.
About the code in these notes: Paper 2 comes in a Python version and a Java version, and both ask the same questions.
The examples here are Python, with the Java equivalent named where it differs. The reasoning is identical in either.
Free preview
This is the free notes preview
You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:
- FlashcardsLock in vocabulary and key terms with spaced repetition.
- Practice questionsAnswer exam-style questions and get instant AI marking.
- Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
- Personalised study planA daily plan built around your exam date and weak areas.
Numbers
- integer — a whole number: a count, an ID, a year
- decimal — a number with a fractional part: a price, a measurement
- Arithmetic works on both; dividing two integers may give a decimal
Text
- char — a single character: 'A', '7', '?'
- string — a sequence of characters: "Hello", "S1"
- '7' the char is not 7 the integer — you cannot add 1 to it
True or false
- Boolean — one of exactly two values: True or False
- Produced by every comparison: x > 5 is a Boolean
- What every if statement and every loop test actually uses
The same symbol, two meanings: + adds numbers and joins strings.
So "12" + "1" gives "121", while 12 + 1 gives 13. Mixing the two is one of the commonest bugs there is.
Feeling unprepared for exams?
Get a clear study plan, practice with real questions, and know exactly where you stand before exam day. No more guessing.
Where a variable can be seen: A local variable exists only inside the function that created it, and disappears when that function ends.
A global variable is created outside any function and can be seen throughout the program.
Local
- Created inside a function
- Cannot be seen or changed from anywhere else
- Destroyed when the function ends
- Two functions may safely use the same name
- The default choice — it keeps effects contained
Global
- Created outside any function
- Visible throughout the program
- Any function might change it, from anywhere
- A bug can come from code you were not looking at
- Use sparingly — for genuine settings and constants
Why globals cause hard bugs: If a value is wrong and it is local, the cause is in that function. If it is global, the cause could be anywhere in the program.
That difference is the entire argument for preferring local variables.
How this is tested — you must track each variable's value, and know where each one can be seen. It comes up two ways:
Paper 2 — working with code
- Trace a program and state the output, 3-5 marks
- Identify a variable's type, or a type error
- Say why a variable is not visible where it is used
Paper 2 — the algorithmic-thinking question
- Reason about scope with no code shown
- Explain why a value did not change as expected
The classic trap: Assuming a function's change to a local variable is visible outside it. It is not — the local variable is destroyed when the function ends, and the outside value never moved.
State the output of this program and explain the final line.
total = 10
def bump(n):
total = n + 1
return total
print(bump(4))
print(total)
Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.
State the output of this program and explain the final line.
int total = 10;
static int bump(int n) {
int total = n + 1;
return total;
}
System.out.println(bump(4));
System.out.println(total);
Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.