Skip to content
Study Hall
← 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 in int() or float() for numbers.
  • / gives a float; // gives integer division; % gives the remainder.
  • "5" + "5" is "55", but 5 + 5 is 10.
  • Flowchart shapes: oval = start/end, parallelogram = input/output, rectangle = process, diamond = decision.
  • n % 2 == 0 is the standard test for an even number.

Selection: if, elif and else

  • if tests a condition; elif is checked only when everything above was False; else catches everything remaining.
  • In Python, indentation defines the block — it is syntax, not formatting.
  • and needs both conditions true; or needs at least one.
  • "Outside a range" uses or; "inside a range" uses and.
  • 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 for when the number of repetitions is known; while when 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.
  • break leaves the loop; continue skips 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] = v replaces 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.
  • return sends a value back to the caller; print only displays it.
  • A function with no return returns None.
  • 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() around input() and then trying arithmetic on a string. (Variables, Data Types and Input/Output)
  • Using / where // is needed, or vice versa. / always gives a float, even 10 / 2 which 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" and 5 display identically and behave completely differently. (Variables, Data Types and Input/Output)
  • Using = instead of == in a condition. (Selection: if, elif and else)
  • Using and when testing whether a value is outside a range — it should be or. (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, elif or else line. (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; use elif if 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 print inside the loop, so it runs every pass instead of once at the end. (Loops: while and for)
  • Using while when the count is known — for handles 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 is len(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 change s. 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 print inside a function where return is 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 def must be executed first. (Functions, Errors and Debugging)
  • Passing arguments in the wrong order, so area(3, 5) and area(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.

  1. Variables, Data Types and Input/Output
    1
    What does input() return when the user types 42?
  2. Variables, Data Types and Input/Output
    2
    What is the value of 23 // 7?
  3. Variables, Data Types and Input/Output
    3
    Which expression tests whether the variable n is divisible by 3?
  4. Variables, Data Types and Input/Output
    4
    What does 3 ** 4 evaluate to?
  5. Variables, Data Types and Input/Output
    5
    Order the stages of a simple program that converts a mark to a percentage.
    1. 1Convert both inputs from strings to numbers
    2. 2Print the result formatted to one decimal place
    3. 3Calculate mark divided by total, times 100
    4. 4Read the mark and the total from the user
  6. Selection: if, elif and else
    6
    How many branches of an if-elif-elif-else chain can execute in one pass?
  7. Selection: if, elif and else
    7
    This chain is written as if m >= 50: Pass, elif m >= 80: Excellent. What does a mark of 85 print?
  8. Selection: if, elif and else
    8
    What is wrong with if mark = 50:?
  9. Selection: if, elif and else
    9
    What does not (x > 5) evaluate to when x = 5?
  10. Selection: if, elif and else
    10
    A program rejects marks under 0 or over 100. Which set of test values best checks it?
  11. Loops: while and for
    11
    How many times does the body of for i in range(3, 12): execute?
    times
  12. Loops: while and for
    12
    A while loop prints the same value forever. What is the most likely cause?
  13. Loops: while and for
    13
    Where must an accumulator be initialized?
  14. Loops: while and for
    14
    Order the parts of a correct while loop.
    1. 1Test the condition
    2. 2Update the control variable
    3. 3Execute the loop body
    4. 4Initialize the control variable
  15. Loops: while and for
    15
    After n = 1 and while n < 50: n = n * 3, what is the final value of n?
  16. Lists and Strings
    16
    For data = [5, 10, 15, 20, 25], what is the value of data[2]?
  17. Lists and Strings
    17
    What does "TEJ3M"[1:3] evaluate to?
  18. Lists and Strings
    18
    A student writes name.upper() and finds name is unchanged. Why?
  19. Lists and Strings
    19
    Which loop correctly adds 5 to every mark in a list, modifying the list itself?
  20. Lists and Strings
    20
    A user types " Yes " and the program compares it to "yes". What is the fix?
  21. Functions, Errors and Debugging
    21
    A function ends with print(result) instead of return result. What does x = my_function() assign to x?
  22. Functions, Errors and Debugging
    22
    What keyword sends a value back from a function to the code that called it?
  23. Functions, Errors and Debugging
    23
    Match each error message to its usual cause.
    NameError
    TypeError
    IndexError
    ZeroDivisionError
  24. Functions, Errors and Debugging
    24
    Order these debugging steps.
    1. 1Read the error message and identify the failing line
    2. 2Change one thing and re-test
    3. 3Add print statements or trace by hand to narrow it down
    4. 4Reproduce the bug with a specific input
  25. Functions, Errors and Debugging
    25
    Why is it better for a function to return a value than to print it?

Endless drill

Fresh numbers every time, for the calculation questions in this unit.

Endless drillnew numbers every time

Generating a problem…

Keep going