Skip to content
Ameen Abozaid
BEG-057

Verifying AI-generated code

  • 5 Lessons
  • 59 min of reading
  • 2.5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Reading and tracing generated code
  • testing it
  • plausible-but-wrong code
  • licensing and academic integrity

Learn first

59 min of reading · 2.5 h of practice (estimate)

Learning outcomes

  1. Read and trace AI-generated code line by line and explain what it does before using it.
  2. Write tests that check generated code against the requirement, including boundaries and invalid input.
  3. Spot plausible-but-wrong code: off-by-one errors, missing edge cases, wrong complexity, invented library calls.
  4. Explain licensing and academic-integrity rules for AI-generated code and disclose AI use where required.

Lessons

  1. 01Generated code is a draft, not an answer12 min read
  2. 02Reading and tracing code you did not write12 min read
  3. 03Testing generated code against the requirement11 min read
  4. 04Licensing, integrity, and disclosure12 min read
  5. 05Demo: catching a subtle bug in generated binary search with tests12 min read

Lab and exercises

Lab

BEG-057-LAB1 · Trust but verify

Outcomes: 1–4 · Time: ~2.5 h

  1. Ask an assistant for three methods from earlier labs: a search, a hash map operation, and a Library borrowing rule.
  2. For each, write the tests first from the requirement (normal, boundary, invalid input); do not paste the generated code yet.
  3. Paste the code, run the tests, and fix every failure. Document at least one defect found (what, how the test caught it, the fix).
  4. Explain the final code in your own words in comments or the README, and add a disclosure line stating the AI use.

Acceptance: tests written before the code is pasted; at least one defect found and documented; final code explained in your own words in comments or the README.

Exercises (assessment: given generated code with hidden defects, find them with tests; score = defects caught and explained)

IDOutcomeTask
BEG-057-EX011Trace this generated code with [3, 4] and write what it returns and what it should: int sum(int[] a){int s=0; for(int i=0;i<a.length-1;i++) s+=a[i]; return s;}
BEG-057-EX022Write four tests, from the requirement only, for int indexOf(int[] sorted, int target) returning -1 when absent.
BEG-057-EX033Name the problem: a generated method calls String.isNullOrEmpty(s).
BEG-057-EX043A generated "find duplicates" method uses List.contains inside a loop. State its complexity and a better approach.
BEG-057-EX052A teammate writes expected values by running the generated code and copying its output. Explain the risk.
BEG-057-EX064Write a two-sentence disclosure for code you drafted with an assistant, and list two things you must never paste into a public assistant.
Show solutions
  • EX01 Returns 3 (loop stops one early, skips the last item); it should return 7. The bound should be i < a.length.
  • EX02 For example: [] with 5 gives -1; [5] with 5 gives 0; [1,3,5,7] with 7 gives 3; [1,3,5,7] with 4 gives -1.
  • EX03 An invented method: String has no static isNullOrEmpty. Use s == null || s.isEmpty() (or isBlank if blank counts).
  • EX04 O(n²) because contains is O(n). Use a HashSet: O(n) time, O(n) space.
  • EX05 The test then copies any bug into the expectations and cannot fail; expected values must come from the requirement.
  • EX06 For example: "Binary search was first drafted with an AI assistant. I wrote the tests from the requirement, found and fixed a boundary bug, and can explain every line." Never paste secrets or credentials, customer or personal data, or private code (check your policy).

Quiz

Outcomes: O1 read and trace · O2 test against the requirement · O3 spot plausible-but-wrong code · O4 licensing, integrity, disclosure.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Before using generated code you should:O1
  2. 2A trace table records:O1
  3. 3Who is responsible for AI-generated code you submit?O1
  4. 4Tests for generated code should be written from the:O2
  5. 5Writing the tests before pasting the code helps because:O2
  6. 6Which input set is best for a search method?O2
  7. 7for (int i = 1; i < a.length; i++) used to visit every item is an example of a(n):O3
  8. 8A call to a method that does not exist in the library is called:O3
  9. 9A generated loop calls list.contains on every item. The risk is:O3
  10. 10Honest disclosure of AI use includes:O4

Practical part

Assessment: given generated code with hidden defects, find them with tests and explain each (score = defects caught and explained), plus lab BEG-057-LAB1 and exercises BEG-057-EX01 to EX06. Pass: at least half of the hidden defects caught and explained, tests written before the code, and at least 8 of 10 quiz answers correct.

Common mistakes

Writing tests that copy the generated code's behavior instead of the requirement; accepting code you cannot explain; ignoring the complexity of the generated solution.

Assessment

Given generated code with hidden defects, find them with tests; score = defects caught and explained.

Career relevance

Reviewing AI-written code is part of every pull request today; JNR-055 applies this to a team workflow.

Type to search levels, phases, modules and lessons.