All posts Coding Assessments

How to Check Student Code for Plagiarism (Python, Java and C)

An essay plagiarism checker is useless on code: rename the variables and it sees a new program. A code plagiarism checker compares the shape of every answer in the class instead. Eight real Python answers, what was flagged, what was missed, and the free tools available to faculty.

Book a free demo or try the 2-minute student test →

Sixty students hand in a program for the same lab question, and two of them look a bit too alike. One has different variable names, new comments and an extra blank line here and there. Is it a copy?

The short answer: you cannot tell by reading sixty programs, and an ordinary plagiarism checker cannot tell either. What can tell you is a code plagiarism checker: a tool that compares every submission in the class with every other one, after removing the things a student can change for free. It gives you a short list of pairs to read. It does not give you a verdict, and it cannot see code that came from the internet or from ChatGPT.

This post shows how one works, runs a real class of eight Python answers through one, and lists the free tools available to faculty.

Why an essay plagiarism checker does not work on code

Essay checkers look for matching sentences. On code, that fails in both directions.

  • It misses real copies. Rename first to largest and arr to values, add two comments, and nearly every line is now different text. To a text checker it is a new program.
  • It flags honest work. Every Python answer to an input question starts with n = int(input()). A text checker sees thirty students "copying" the same line.

So a checker for code has to compare what the program does, not how it is spelled.

How a code plagiarism checker works

The method most tools use comes from MOSS, the checker Stanford has offered to teachers since the 1990s. It has four steps.

  1. Remove what costs nothing to change. Comments, blank lines and spacing are deleted.
  2. Replace every name with the same placeholder. All variable and function names become one token, and so do all numbers and all text in quotes. Keywords like if, for and return stay. What is left is the shape of the program.
  3. Fingerprint short runs of that shape. Each run of five tokens is turned into a number, and a sample of those numbers becomes the program's fingerprint.
  4. Ignore what the whole class shares. If most of the class has the same run (reading the input, a standard loop), it is the question, not copying, and it is dropped before any two students are compared.

Step 4 is the one that makes the results usable. Without it, every pair in the class scores high, because most of any two answers to the same question is the question.

A student changesDoes it hide a copy?
Variable and function namesNo. All names become the same placeholder.
Comments and blank linesNo. They are removed first.
The messages in print statementsNo. All quoted text becomes one token.
Numbers, such as a limit of 100 changed to 1000No. All numbers become one token.
The whole approach, for example a loop rewritten as a sortYes. But a student who can do that has understood the program.

See this in your own lab. Teemly sets coding tests from your question bank, marks every program against test cases, and explains the results in plain English. We set it up free.

Book a free demo

A worked example: eight Python answers to one lab question

The question, as set in a first-year lab:

Read n, then n integers. Print the second largest distinct value, or -1 if there is none.

Eight students answered in Python. Six wrote their own programs, each a bit differently: sorting a set, two passes with max, a sort in reverse, heapq, and so on. Two did not:

  • Kabir took Diya's program, renamed every variable, added comments and moved the blank lines.
  • Vikram pasted Sana's function into a longer program of his own, with his own input reading and a main().

All eight were run through the similarity check Teemly uses on coding tests, which follows the four steps above. That made 28 pairs. Eleven fingerprints were shared by five or more of the eight students, so they were dropped as part of the question. Pairs scoring under 35% are not listed. Three pairs were:

PairSimilarityMatching linesWhat actually happened
Diya and Kabir100%Diya 2–14, Kabir 5–10 and 12–22A renamed copy
Sana and Vikram79.2%Sana 1–11, Vikram 11–21A pasted function
Aarav and Ishaan54.5%Aarav 4–7, Ishaan 4–7Nothing. Two honest answers.

The other 25 pairs scored between 0% and 27.3%. Ten of them shared nothing at all.

Diya and Kabir: renaming changed nothing

Here is the core of each program. Every name is different, and Kabir's has comments Diya's does not.

  # Diya
  for x in arr:
      if first is None or x > first:
          second = first
          first = x
      elif x != first and (second is None or x > second):
          second = x

  # Kabir
  for v in values:
      # new maximum found
      if largest is None or v > largest:
          runner_up = largest
          largest = v
      # between the two
      elif v != largest and (runner_up is None or v > runner_up):
          runner_up = v

Once names become placeholders and comments are gone, these are the same program, token for token. That is why the score is 100%, and why renaming is the least effective disguise there is.

Sana and Vikram: why there are two numbers

Vikram's program is nearly twice as long as Sana's, because he wrapped her function in his own code. A tool that asked "how much of the two programs together is shared?" would score this pair at 31.1%, low enough to miss. The useful question is "how much of the smaller program is inside the larger one?", and the answer is 79.2%. The matching lines show exactly where: Sana's whole function, lines 1 to 11, sits at lines 11 to 21 of Vikram's program.

When you choose a checker, check that it reports this second kind of score. Pasting a function into a longer file is the most common copy in a lab, and it is the one the simple kind of score hides.

Aarav and Ishaan: a flag that was not copying

Aarav sorted a set. Ishaan used heapq. They solved the problem differently, but both ended with the same four lines:

  # Aarav
  if len(unique) < 2:
      print(-1)
  else:
      print(unique[-2])

  # Ishaan
  if len(nums) < 2:
      print(-1)
  else:
      print(heapq.nlargest(2, nums)[1])

Only two of the eight wrote that ending, so it was not shared widely enough to count as part of the question, and in a short program four lines is half the fingerprint.

This is the most important thing to know about any code plagiarism checker. A high score means "read these two side by side", never "these students cheated". Short programs written to the same question genuinely look alike. One look at these two answers settles it.

What a code plagiarism checker cannot catch

  • Code from ChatGPT or the internet, used by one student. The checker compares the class with itself. A program nobody else in the class handed in matches nothing. For what to do about ChatGPT, see how faculty can use AI now that students use ChatGPT.
  • Help given out loud. A friend explains the logic and the student types it their own way. The shapes differ, so nothing is flagged.
  • Very short answers. A two-line program can only be written one way, so Teemly does not compare answers under 40 tokens. Reporting them would teach you to ignore the check.
  • Tiny groups. With fewer than four answers in one language, there is no class to tell "the question" from "a copy", so those answers are not compared.

Python, Java and C: does the language matter?

The method is the same for every language. Only the list of keywords changes. Teemly checks Python, Java, C, C++ and JavaScript this way.

A checker should only compare answers in the same language. A Python answer and a Java answer to the same question share nothing a checker can see, and a 0% between them would look like proof of honest work when no comparison was really made. So in a lab where students may choose their language, each language is checked as its own group.

Free code plagiarism checkers for faculty

You do not need to buy anything to check a lab's code. These are the options:

ToolCostHow you use itWhere the code goes
MOSS (Stanford)Free for teachingAsk for an account by email, then send the files with a scriptStanford's server. Results are on a web page anyone with the link can open.
JPlagFree, open sourceDownload it and run it on your own computer (needs Java)Stays on your computer
Dolos (Ghent University)Free, open sourceUpload the files in a browser, or host it yourselfTheir server, unless you host it
TeemlyPart of every planRuns on every coding test by itself. Nothing to upload.Stays in your institution's account

The first three work on any set of files: you collect the programs, put them in a folder and run the check. Teemly's check only covers answers written in a Teemly test, but there is nothing to collect. Open the paper in Reports, Tests and the Similar code tab lists the pairs, with the matching lines and each student's paste and tab-switch count beside them.

That last part matters more than the score. "These two answers are 79% alike" is a reason to look. "79% alike, and one of them tried to paste eleven times" is a reason to talk to the student.

What to do with a flagged pair

  1. Read the two answers side by side, at the lines the checker names. Most flags settle here, one way or the other, as Aarav and Ishaan's did.
  2. Check how the test was sat. Paste attempts and tab switches are recorded on each paper. See what a browser can and cannot catch during a test.
  3. Ask each student to explain their code. Two minutes, one at a time: "Why does this line set second = first?" A student who wrote it can answer. This is fairer and quicker than any score.
  4. Write down what you decided, even when the answer is "nothing". In Teemly the review is recorded on the paper: who looked, when, and what they decided.

And for the next lab, the best prevention is in the question. A question with hidden test cases, run in the lab under a time limit, leaves little room to copy and little reason to.

The short version

Use a code plagiarism checker, not an essay one. Trust it to find renamed copies and pasted functions, which it does well. Do not trust a score on its own: read the pair, check how the test was sat, and ask the student. And remember it compares the class with itself, so it will never see code that came from outside the room.

To see a coding test from the student's side, the two-question sample test at teemly.in/try needs no sign-up. To run your next lab with the similarity check switched on, see Teemly's coding assessments.

Free PDF checklist

Not ready for a demo yet?

Get the Assessment Readiness Checklist: 42 questions to work through before you trust your next exam to a digital platform.

Get the free checklist
Free demo

Run your next coding lab on Teemly

We set it up for your department, free. Every program is marked against test cases, and Teemly AI explains the results in plain English.

Book a demo!