Emerging trend: AI code generation tools

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

Emerging trend: AI code generation tools

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does an AI code generation tool turn into program code?
A A plain-English (natural-language) prompt
B A compiled machine-code file
2. Which one is an example of a code assistant in an editor?
A A spell checker that fixes essays in a word processor
B Code completion that suggests the next lines as you type
3. Chat-based code generation works by:
A Typing a request into a chatbot and getting code back in the reply
B Dragging blocks onto a canvas to build the code by hand
4. AI is generally good at which task in programming?
A Drafting repetitive boilerplate code quickly
B Guaranteeing code is free of every bug
5. AI-generated code should be:
A Trusted and used without checking because AI is never wrong
B Checked and tested by the programmer before use

Match the Pairs

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

Left columnRight column
1# Write a function that returns the average of a list ATrusting AI-generated code without checking or testing it. It can contain logic errors, bugs or out-of-date methods.
2Tab key to accept a suggestion BA natural-language prompt written as a comment, which an editor assistant can turn into a suggested function
3Chat-based code generation: describe the task in plain English and the chatbot replies with program code CAccepting the code that an editor's AI code completion tool has proposed as you type
4AI is good at repetitive boilerplate code, but the programmer must still test the result Dassert average([2, 4, 6]) == 4
5# AI wrote this, so it must be correct. Submit it without running it. EThe prompt is vague. AI tools generate better code from clear, specific prompts that state the language, inputs and expected output.
6Prompt: Make my app better FPrompt: Write a Python function that checks if a number is prime

Definitions

AI code generation

Fill in the Blanks

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

1.
# Prompt: add two numbers ___ add(a, b): return a + b
2.
# Generated by an AI assistant def area(w, _): return w * h
3.
prompt = input("Ask: ") reply = chatbot(prompt) _____(reply)
4.
def double(n): return n * 2 ______ double(4) == 8
5.
result = average([2, 4, 6]) if result __ 4: print("Test passed")

Spot the Error

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

Question 1
A def average(nums):
B total = sum(nums)
C return total / len(nums)
D print(average(2, 4, 6))
Question 2
A prompt = input('Describe: ')
B words = prompt.split()
C count = len(words)
D print('Words:' count)
Question 3
A def is_even(n):
B remainder = n % 2
C return remainder = 0
D print(is_even(10))
Question 4
A tips = ['test', 'review']
B for tip in tips
C print(tip)
Question 5
A def greet(name):
B return 'Hello, ' + nme
C print(greet('Sam'))

Answer Key

Emerging trend: AI code generation tools
Section A: Quick Questions
Q1
A — A plain-English (natural-language) prompt
Q2
B — Code completion that suggests the next lines as you type
Q3
A — Typing a request into a chatbot and getting code back in the reply
Q4
A — Drafting repetitive boilerplate code quickly
Q5
B — Checked and tested by the programmer before use
Section B: Match the Pairs
Q1
# Write a function that returns the average of a list → A natural-language prompt written as a comment, which an editor assistant can turn into a suggested function
Q2
Tab key to accept a suggestion → Accepting the code that an editor's AI code completion tool has proposed as you type
Q3
Chat-based code generation: describe the task in plain English and the chatbot replies with program code → Prompt: Write a Python function that checks if a number is prime
Q4
AI is good at repetitive boilerplate code, but the programmer must still test the result → assert average([2, 4, 6]) == 4
Q5
# AI wrote this, so it must be correct. Submit it without running it. → Trusting AI-generated code without checking or testing it. It can contain logic errors, bugs or out-of-date methods.
Q6
Prompt: Make my app better → The prompt is vague. AI tools generate better code from clear, specific prompts that state the language, inputs and expected output.
Section C: Definitions
Q1
AI code generation — Using AI tools, like editor assistants and chatbots, to turn plain-English prompts into program code. It saves time, but the code must still be checked and tested by the programmer.
Section D: Fill in the Blanks
Q1
# Prompt: add two numbers def add(a, b): return a + b
Q2
# Generated by an AI assistant def area(w, h): return w * h
Q3
prompt = input("Ask: ") reply = chatbot(prompt) print(reply)
Q4
def double(n): return n * 2 assert double(4) == 8
Q5
result = average([2, 4, 6]) if result == 4: print("Test passed")
Section E: Spot the Error
Q1
D — The AI-generated function takes one list, but this call passes three separate numbers. It should be print(average([2, 4, 6])).
Q2
D — A comma is missing between the two arguments, which causes a syntax error. It should be print('Words:', count).
Q3
C — A single = assigns a value and is not allowed in a return. To compare, the AI code should use ==: return remainder == 0.
Q4
B — A for statement must end with a colon. It should be for tip in tips:. Reading AI code carefully helps you spot small syntax slips like this.
Q5
B — The variable nme does not exist, so Python raises a NameError. The parameter is called name. AI code can contain typos, so check it.