Recognise the most common novice programming misconceptions and diagnose them from a student's code.
Why it matters
When a student's program is wrong, it is tempting to fix the line and move on. But most novice errors are not typos: they come from a consistent, wrong mental model of how the computer works. Fix the line and the model stays; the same error comes back next week in a different form. Good CS teachers learn to read a wrong answer as evidence of what the student believes, then target that belief.
The core idea: notional machines
Researchers use the term notional machine for the simplified model of the computer that a teacher wants students to hold: "a variable is a named box holding one value", "the program runs one line at a time, top to bottom, unless a loop or call sends it elsewhere". Misconceptions are gaps or errors in that model. Making the notional machine explicit (with diagrams, tracing tables and visualisers) is one of the most effective things you can do.
Common misconceptions and how they show up
| Misconception | What the student believes | Typical evidence |
|---|---|---|
| Assignment as equation | a = b links a and b forever | expects a to change when b changes later |
| Variables hold history | a variable remembers all its past values | traces show several values in one box |
| Order does not matter | the computer reads the whole program and "understands" it | uses a variable before assigning it |
| The computer knows what I mean | names carry meaning (largest will find the largest) | names a variable and expects behaviour |
| Print equals return | showing a value is the same as giving it back | function prints but caller gets None |
if loops | an if repeats like while | expects an if body to run many times |
| Loop runs once more / less | unclear about where range stops | off-by-one errors on boundaries |
= vs == | the symbols are interchangeable | assignment inside a condition (in languages that allow it) |
Worked example: diagnosing from code
A student writes this to swap two values:
a = 5
b = 9
a = b
b = a
print(a, b) # student expects: 9 5
The output is 9 9. Possible beliefs behind it:
- Assignment as equation or simultaneous: the student thinks both lines happen "at once", like swapping in maths.
- Variables hold history: they think
astill "remembers" 5 aftera = b.
A good diagnostic question separates them: "After line 3, what is in a? Is 5 stored anywhere?" A student who says "5 is still in a as well" has the history misconception. Then you address the model, not just the code: trace line by line in a table, one row per line, one column per variable.
| line | a | b |
|---|---|---|
| 1 | 5 | - |
| 2 | 5 | 9 |
| 3 | 9 | 9 |
| 4 | 9 | 9 |
Only after the model is fixed do you introduce a temporary variable (temp = a).
Worked example: print vs return
def double(n):
print(n * 2)
result = double(4) + 1
The program shows 8 and then crashes with TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'. The student is confused: "But it printed 8!" The misconception is that printing gives a value back. Ask: "Where does 8 go when it is printed? Where does the value go when it is returned?" A picture of the screen versus the caller helps.
Teaching moves that help
- Predict before run. Ask students to predict output before running code. Wrong predictions surface misconceptions quickly.
- Trace tables and visualisers. Python Tutor (pythontutor.com) shows variables and frames step by step.
- Diagnostic multiple-choice questions where each wrong answer matches a known misconception, so the choice tells you the belief.
- Refutation. Show a case where the wrong model predicts something different from what happens, then explain why.
Common mistakes (for teachers)
- Fixing the code for the student. You remove the evidence and keep the misconception.
- Assuming it is carelessness. Consistent errors usually mean a consistent model.
- Explaining with too much abstraction. "Memory addresses" can confuse beginners; start with boxes and arrows.
- Ignoring language effects.
=reads as "equals" in maths. Say "gets" or "is assigned" aloud.
Quick recap
- Most novice errors come from wrong mental models, not typos.
- Make the notional machine explicit with tracing tables and visualisers.
- Learn the common misconceptions: assignment as equation, print vs return,
ifas loop, off-by-one. - Diagnose with targeted questions and predictions before you correct.