AI-based code generators

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

AI-based code generators

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which describes an AI code generator?
A A tool that only fixes spelling mistakes in comments
B A tool that writes code from a plain-language description
2. A student types 'make a function that averages a list' into a chat tool. What is this?
A A chat-based code generator
B A code completion feature
3. As you type def calc_total( in your editor, grey text suggests the rest of the line. What is this?
A A chat-based generator
B Code completion
4. What can an AI code generator do?
A Draft code quickly that you can then test and edit
B Guarantee the code has no errors
5. What can an AI code generator NOT do?
A Take responsibility for whether the code is correct
B Suggest code from a short description

Match the Pairs

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

Left columnRight column
1Write a Python function that checks if a number is prime Aresult = ai_draft(); run_tests(result)
2def total_price(items): # editor suggests next line BThe description is too vague. A generator needs a clear, detailed plain-language request to give useful code.
3An AI code generator can save time by writing a first draft of code, but a person must still test it CA chat-based generator turns a plain-language request like this into program code
4An AI code generator cannot be trusted to know your exact needs, so you must give a clear description DUsing AI code without checking it. A person must still read, test and fix the generated code.
5# AI wrote this, so it must be correct use_code(ai_output) Eprompt = "Write a function that adds GST to a price"
6prompt = "Make my game" FCode completion in an editor suggests the next lines as you type

Definitions

AI code generator

Fill in the Blanks

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

1.
# AI suggestion: add two numbers def total(a, b): ______ a + b
2.
# Prompt: show every item in a list for item in items: _____(item)
3.
# Test the AI code before using it result = total(2, 3) ______ result == 5
4.
# AI suggestion: check for an adult if age __ 18: print("Adult")
5.
# AI suggestion: count from 1 to 5 for i in _____(1, 6): print(i)

Spot the Error

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

Question 1
A # AI-made: add two numbers
B def add(a, b):
C return a + b
D print(add(2, 3)
Question 2
A def average(nums):
B total = sum(nums)
C return total * len(nums)
Question 3
A def is_even(n):
B return n % 2 == 0
C if is_even[4]:
D print("Even")
Question 4
A function greet(name):
B return "Hi " + name
C print(greet("Sam"))
Question 5
A def double(x):
B return x * 2
C assert double(4) = 8
D print("Test passed")

Answer Key

AI-based code generators
Section A: Quick Questions
Q1
B — A tool that writes code from a plain-language description
Q2
A — A chat-based code generator
Q3
B — Code completion
Q4
A — Draft code quickly that you can then test and edit
Q5
A — Take responsibility for whether the code is correct
Section B: Match the Pairs
Q1
Write a Python function that checks if a number is prime → A chat-based generator turns a plain-language request like this into program code
Q2
def total_price(items): # editor suggests next line → Code completion in an editor suggests the next lines as you type
Q3
An AI code generator can save time by writing a first draft of code, but a person must still test it → result = ai_draft(); run_tests(result)
Q4
An AI code generator cannot be trusted to know your exact needs, so you must give a clear description → prompt = "Write a function that adds GST to a price"
Q5
# AI wrote this, so it must be correct use_code(ai_output) → Using AI code without checking it. A person must still read, test and fix the generated code.
Q6
prompt = "Make my game" → The description is too vague. A generator needs a clear, detailed plain-language request to give useful code.
Section C: Definitions
Q1
AI code generator — A tool that writes program code from a plain-language description or by suggesting the next lines as you type. It can save time, but a person must still check the code.
Section D: Fill in the Blanks
Q1
# AI suggestion: add two numbers def total(a, b): return a + b
Q2
# Prompt: show every item in a list for item in items: print(item)
Q3
# Test the AI code before using it result = total(2, 3) assert result == 5
Q4
# AI suggestion: check for an adult if age >= 18: print("Adult")
Q5
# AI suggestion: count from 1 to 5 for i in range(1, 6): print(i)
Section E: Spot the Error
Q1
D — The print call is missing its closing bracket, so the code will not run. It should be print(add(2, 3)). Check AI-generated code for small syntax slips like this.
Q2
C — An average is the total divided by the count, so it should use / not *. A code completion suggestion can look right but be wrong, so a person must check it.
Q3
C — A function is called with round brackets, not square brackets. It should be if is_even(4):. Chat-based generators can make small mistakes, so run and read the code.
Q4
A — Python defines a function with def, not function. That is JavaScript. AI code generators can mix up languages, so check the code matches the language you asked for.
Q5
C — A single = assigns a value; it does not compare. The test should use == to compare. Testing AI-generated code like this shows whether it works.