← Unit 5: Computer Programming
TEJ3M · Unit 5 test review
Computer Programming: Test Review
Everything for the Unit 5 test on one page: the key facts from all 5 lessons, the mistakes that cost the most marks, and a practice test that points you back to the lessons you need.
Key facts
Variables, Data Types and Input/Output
=assigns a value;==tests equality.input()always returns a string — wrap it inint()orfloat()for numbers./gives a float;//gives integer division;%gives the remainder."5" + "5"is"55", but5 + 5is10.- Flowchart shapes: oval = start/end, parallelogram = input/output, rectangle = process, diamond = decision.
n % 2 == 0is the standard test for an even number.
Selection: if, elif and else
iftests a condition;elifis checked only when everything above was False;elsecatches everything remaining.- In Python, indentation defines the block — it is syntax, not formatting.
andneeds both conditions true;orneeds at least one.- "Outside a range" uses
or; "inside a range" usesand. - An if-elif chain runs at most one branch; separate ifs can all run.
- Order an if-elif chain from the most specific condition to the least.
- Test at the boundary, one below and one above.
Loops: while and for
- Use
forwhen the number of repetitions is known;whilewhen waiting for a condition. range(n)gives 0 to n−1 — n values.range(a, b)stops before b.- Every while loop needs initialize, test, and update.
- Initialize counters and accumulators BEFORE the loop, never inside it.
- A nested loop runs the inner loop fully for every pass of the outer loop.
breakleaves the loop;continueskips to the next pass.
Lists and Strings
- List indexes start at 0; the last valid index is
len(list) - 1. len()gives the number of items;range(len(x))gives exactly the valid indexes.append()adds to the end;marks[i] = vreplaces an existing item.for item in list:gives values;for i in range(len(list)):gives indexes.- Strings are immutable — methods return a new string rather than changing the original.
- Slices stop before the end index:
s[1:4]gives characters 1, 2 and 3. - Always
.strip()text input before comparing it.
Functions, Errors and Debugging
- A parameter is the name in the definition; an argument is the value passed in the call.
returnsends a value back to the caller;printonly displays it.- A function with no
returnreturnsNone. - Local variables exist only inside their function and cannot clash with names outside.
- Syntax errors stop the program before it starts; runtime errors stop it mid-run; logic errors do not stop it at all.
- Read the LAST two lines of a traceback first.
- Test with normal, boundary and invalid inputs.
Where marks get lost
- Using
=where==is needed in a condition — Python raises a syntax error, and other languages silently assign instead. (Variables, Data Types and Input/Output) - Forgetting
int()aroundinput()and then trying arithmetic on a string. (Variables, Data Types and Input/Output) - Using
/where//is needed, or vice versa./always gives a float, even10 / 2which gives 5.0. (Variables, Data Types and Input/Output) - Naming variables
x,a,temp2— future you will not remember what they held. (Variables, Data Types and Input/Output) - Printing without units or context, so the output is a bare number the user cannot interpret. (Variables, Data Types and Input/Output)
- Assuming a value's type from how it looks on screen.
"5"and5display identically and behave completely differently. (Variables, Data Types and Input/Output) - Using
=instead of==in a condition. (Selection: if, elif and else) - Using
andwhen testing whether a value is outside a range — it should beor. (Selection: if, elif and else) - Ordering an if-elif chain from the lowest threshold up, so high values match an early branch. (Selection: if, elif and else)
- Forgetting the colon at the end of an
if,eliforelseline. (Selection: if, elif and else) - Inconsistent indentation, which either raises an error or silently moves a line out of the block. (Selection: if, elif and else)
- Putting a condition on
else. It takes no condition; useelifif you need one. (Selection: if, elif and else) - Testing only the middle of each range and never the boundary value itself. (Selection: if, elif and else)
- Using
range(1, 100)when you need 1 to 100 — it stops at 99. (Loops: while and for) - Forgetting to update the control variable in a while loop, producing an infinite loop. (Loops: while and for)
- Initializing an accumulator inside the loop, which resets it every pass. (Loops: while and for)
- Indenting the final
printinside the loop, so it runs every pass instead of once at the end. (Loops: while and for) - Using
whilewhen the count is known —forhandles initialization and updating for you. (Loops: while and for) - Forgetting that a nested loop multiplies the work, not adds to it. (Loops: while and for)
- Using
list[len(list)]— the last valid index islen(list) - 1. (Lists and Strings) - Looping with
range(len(x) + 1), which crashes with IndexError on the final pass. (Lists and Strings) - Starting a maximum search at 0 when the data can be negative. (Lists and Strings)
- Expecting
s.upper()to changes. Strings are immutable — you must assign the result. (Lists and Strings) - Forgetting that a slice stops before its end index. (Lists and Strings)
- Comparing raw user input without
.strip()and.lower(). (Lists and Strings) - Dividing by
len(items)without checking for an empty list, which raises ZeroDivisionError. (Lists and Strings) - Using
printinside a function wherereturnis needed, so the caller receives None. (Functions, Errors and Debugging) - Expecting a variable created inside a function to be visible outside it. (Functions, Errors and Debugging)
- Calling a function before defining it — in Python the
defmust be executed first. (Functions, Errors and Debugging) - Passing arguments in the wrong order, so
area(3, 5)andarea(5, 3)silently mean different things. (Functions, Errors and Debugging) - Reading only the first line of a traceback. The last two lines carry the useful information. (Functions, Errors and Debugging)
- Changing several things at once while debugging, then not knowing which change fixed it. (Functions, Errors and Debugging)
- Testing only with input that works, so boundary and invalid cases fail in front of an audience. (Functions, Errors and Debugging)
Practice test
25 questions from across the unit. Each one shows the lesson it comes from.
- Variables, Data Types and Input/Output1What does
input()return when the user types 42? - Variables, Data Types and Input/Output2What is the value of
23 // 7? - Variables, Data Types and Input/Output3Which expression tests whether the variable
nis divisible by 3? - Variables, Data Types and Input/Output4What does
3 ** 4evaluate to? - Variables, Data Types and Input/Output5Order the stages of a simple program that converts a mark to a percentage.
- 1Convert both inputs from strings to numbers
- 2Print the result formatted to one decimal place
- 3Calculate mark divided by total, times 100
- 4Read the mark and the total from the user
- Selection: if, elif and else6How many branches of an if-elif-elif-else chain can execute in one pass?
- Selection: if, elif and else7This chain is written as
if m >= 50:Pass,elif m >= 80:Excellent. What does a mark of 85 print? - Selection: if, elif and else8What is wrong with
if mark = 50:? - Selection: if, elif and else9What does
not (x > 5)evaluate to when x = 5? - Selection: if, elif and else10A program rejects marks under 0 or over 100. Which set of test values best checks it?
- Loops: while and for11How many times does the body of
for i in range(3, 12):execute?times - Loops: while and for12A while loop prints the same value forever. What is the most likely cause?
- Loops: while and for13Where must an accumulator be initialized?
- Loops: while and for14Order the parts of a correct while loop.
- 1Test the condition
- 2Update the control variable
- 3Execute the loop body
- 4Initialize the control variable
- Loops: while and for15After
n = 1andwhile n < 50: n = n * 3, what is the final value of n? - Lists and Strings16For
data = [5, 10, 15, 20, 25], what is the value ofdata[2]? - Lists and Strings17What does
"TEJ3M"[1:3]evaluate to? - Lists and Strings18A student writes
name.upper()and findsnameis unchanged. Why? - Lists and Strings19Which loop correctly adds 5 to every mark in a list, modifying the list itself?
- Lists and Strings20A user types " Yes " and the program compares it to
"yes". What is the fix? - Functions, Errors and Debugging21A function ends with
print(result)instead ofreturn result. What doesx = my_function()assign to x? - Functions, Errors and Debugging22What keyword sends a value back from a function to the code that called it?
- Functions, Errors and Debugging23Match each error message to its usual cause.
NameErrorTypeErrorIndexErrorZeroDivisionError - Functions, Errors and Debugging24Order these debugging steps.
- 1Read the error message and identify the failing line
- 2Change one thing and re-test
- 3Add print statements or trace by hand to narrow it down
- 4Reproduce the bug with a specific input
- Functions, Errors and Debugging25Why is it better for a function to
returna value than toprintit?
Endless drill
Fresh numbers every time, for the calculation questions in this unit.
Endless drillnew numbers every time
Generating a problem…