arrow_backBack to field notes
PYTHON Published 4 Aug 2026

Programming for the Exam: Python Answers That Score

How to write Python code on exams that actually earns full marks — structure, edge cases, and time management under pressure.

Exam graders don't run your code through a debugger for twenty minutes. They read it once, maybe twice, and check it against a rubric. A working solution that ignores the rubric can still lose points, while a slightly rougher one that hits every checkpoint can score higher. Writing Python for an exam is a different skill from writing Python for a real project, and it's worth practicing separately.

Read the rubric before you read the problem twice

Most programming exams — AP CS A style free-response questions, university midterms, coding bootcamp assessments — publish partial-credit rules. A typical rubric line looks like: "1 point for correct loop structure, 1 point for correct return value, 1 point for handling the empty list case." If you write a dense one-liner that gets the right answer but skips an explicit loop, some graders will dock you even though the output is correct.

Before typing anything, identify the pieces a grader is likely to check separately:

  • Function signature matches exactly what was asked (name, parameter order, return type)
  • Base case and recursive case, if recursion is expected
  • Loop bounds (off-by-one errors are the single most common point loss)
  • Edge cases: empty input, single-element input, negative numbers, duplicates

Write these as comments first. # handle empty list, # base case: n == 0. This costs ten seconds and guarantees you don't forget the case a grader is specifically looking for.

Structure code so partial credit is visible

Compare two answers to "write a function that returns the second largest number in a list":

def second_largest(nums):
    return sorted(nums)[-2]
def second_largest(nums):
    if len(nums) < 2:
        return None
    largest = second = float('-inf')
    for n in nums:
        if n > largest:
            second = largest
            largest = n
        elif n > second and n != largest:
            second = n
    return second

The first is correct for well-formed input but crashes on a list with fewer than two elements and gives a wrong answer on duplicates like [5, 5, 3] (returns 5, arguably wrong depending on the spec). The second is longer but every rubric line — edge case, correct logic, correct return — is explicit and gets its own line a grader can check off. On a timed exam, the second version scores higher almost every time, even though it takes longer to type.

Don't optimize prematurely, don't leave obvious bugs either

Exam questions rarely test Big-O knowledge unless they say so directly. If the prompt says "write a function that finds duplicates," a nested loop at O(n²) is fine unless the prompt specifies large input or asks for an efficient solution. Spending exam time converting to a set-based O(n) approach when it wasn't asked for is time you don't get back.

That said, some errors will always cost you regardless of the rubric:

  • Using == instead of is inconsistently causing logic bugs (rare in Python exams but shows up in comparison-heavy questions)
  • Mutating a list while iterating over it — for x in lst: lst.remove(x) is a classic exam trap that produces silently wrong output
  • Forgetting return and printing instead — many autograders check return values, not stdout
  • Off-by-one in range()range(len(nums)) vs range(len(nums) - 1) is worth double-checking every single time you write it

Trace your code by hand before submitting

On paper exams and most in-browser coding exams, you don't get to run the code. Pick one small example and trace it line by line, writing down variable values as you go. This catches maybe 80% of logic errors in under two minutes, and it's the single highest-value thing you can do with leftover time.

For recursive functions specifically, trace at least two levels deep and explicitly write out the base case hit. Graders often give a point just for demonstrating the base case terminates correctly, separate from the point for the recursive case being correct.

Manage the clock like a resource, not an afterthought

If a free-response section is worth 9 points across three sub-questions, budget roughly equal time and move on if you're stuck past that budget — a half-written but nearly correct part (b) is worth more raw points than a perfect part (a) and a blank part (c). Write a stub function with the right signature and a pass or a guess even if you can't finish the logic; a correct signature alone is sometimes worth a rubric point on its own.

For more on writing clean, testable Python outside the exam room, and on data structures that show up constantly in these questions, check the related Python and Computer Science segments on Korra Studio.

Written with AI assistance, reviewed and published by Michal Pilch (CISSP), Korra Studio.

Ready to go further?

This is one note from the Korra Studio knowledge base — the platform pairs every topic with 1-to-1 mentoring.

Get started freearrow_forward