Checking and testing AI-generated code

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Checking and testing AI-generated code

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. An AI tool gives you code that looks neat and runs without errors. What should you do before using it?
A Trust it, because code that runs must be correct
B Trace it and test it with your own test data
2. Your requirement says a mark must be accepted from 0 to 100. Which test data best checks AI-generated validation code?
A -1, 0, 100 and 101
B Only 50, because it is a typical value
3. The AI wrote: if mark > 0 and mark < 100. The requirement says 0 to 100 inclusive. What is the problem?
A It rejects the boundary values 0 and 100
B It is fine, because it accepts all marks
4. AI code contains the line: total = math.sum_all(prices). You cannot find sum_all in Python's documentation. What is the most likely issue?
A The AI made up a function that does not exist
B Your Python needs to be restarted to find it
5. Why is tracing AI-generated code with a trace table useful?
A It makes the code run faster
B It shows how variable values change and reveals logic errors

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1assert is_adult(18) == True A| i | total | | 1 | 5 |
2import easy_grader BA possible made-up library: check that it really exists before trusting the AI code that uses it
3Trace the AI code by hand with sample values, recording each variable as the lines run CThe AI code fails the requirement at the boundary: age 18 should be accepted, so it should use >= instead of >.
4Test AI code with invalid data to see if it copes with bad input Dcalculate_average("abc")
5# AI code ran with no errors, so it is correct. Ship it! ERunning without errors does not prove the code is correct. It can still give wrong results, so it needs tracing and testing with your own data.
6if age > 18: # requirement: 18 or over FA test with your own boundary value: checks the AI function gives the right result at exactly the cut-off age

Definitions

Verify AI-generated code

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
______ grade(50) == "P"
2.
assert valid(-1) is _____
3.
try: result = int("abc") except __________:
4.
print(_______(str, "reverse"))
5.
mark = 100 print(0 <= ____ <= 100)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def is_pass(mark):
B return mark > 50
C print(is_pass(50))
Question 2
A import math
B r = math.squareroot(49)
C print(r)
Question 3
A def double(n):
B return n * 2
C assert double(4) == 6
Question 4
A from random import randdice
B roll = randint(1, 6)
C print(roll)
Question 5
A total = 0
B for n in [2, 4, 6]:
C total = n
D print(total)

Answer Key

Checking and testing AI-generated code
Section A: Quick Questions
Q1
B — Trace it and test it with your own test data
Q2
A — -1, 0, 100 and 101
Q3
A — It rejects the boundary values 0 and 100
Q4
A — The AI made up a function that does not exist
Q5
B — It shows how variable values change and reveals logic errors
Section B: Match the Pairs
Q1
assert is_adult(18) == True → A test with your own boundary value: checks the AI function gives the right result at exactly the cut-off age
Q2
import easy_grader → A possible made-up library: check that it really exists before trusting the AI code that uses it
Q3
Trace the AI code by hand with sample values, recording each variable as the lines run → | i | total | | 1 | 5 |
Q4
Test AI code with invalid data to see if it copes with bad input → calculate_average("abc")
Q5
# AI code ran with no errors, so it is correct. Ship it! → Running without errors does not prove the code is correct. It can still give wrong results, so it needs tracing and testing with your own data.
Q6
if age > 18: # requirement: 18 or over → The AI code fails the requirement at the boundary: age 18 should be accepted, so it should use >= instead of >.
Section C: Definitions
Q1
Verify AI-generated code — Read and trace AI code, then test it with your own boundary and invalid data, and check it meets the requirements. It can look right but be wrong or use made-up functions.
Section D: Fill in the Blanks
Q1
assert grade(50) == "P"
Q2
assert valid(-1) is False
Q3
try: result = int("abc") except ValueError:
Q4
print(hasattr(str, "reverse"))
Q5
mark = 100 print(0 <= mark <= 100)
Section E: Spot the Error
Q1
B — The requirement says a mark of 50 passes. Testing the boundary value 50 returns False with >, so the AI code is wrong. It should be >=.
Q2
B — math.squareroot() does not exist. AI tools can make up functions that look real. Check the documentation. The real function is math.sqrt().
Q3
C — The expected result must be worked out by hand from the requirement, not copied from the AI. Doubling 4 gives 8, so the test should expect 8.
Q4
A — randdice is a made-up function that the random module does not have. The real function is randint, so check that imports exist before trusting AI code.
Q5
C — Tracing shows total is overwritten each loop, so it ends at 6 instead of 12. To add up the values, the line should be total += n.