This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python prompt templates and structured output, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
Which function safely parses a JSON model response?
Answer: json.loads
Why: json.loads parses JSON data and rejects malformed syntax without executing code.
Does valid JSON guarantee the response has the required fields?
Answer: No; the schema and types still need validation
Why: A JSON object can parse successfully and still omit fields or use the wrong types.
What is a prompt template useful for?
Answer: Keeping instructions stable while filling variable parts
Why: A template separates fixed instructions from per-request data and makes the contract easier to review.
What is the output?
import json
raw = '{"title": "Loops", "level": "beginner"}'
data = json.loads(raw)
required = ["title", "level", "minutes"]
print([key for key in required if key not in data])Answer: ['minutes']
Why: The response has title and level, but it is missing the required minutes key.
Complete the JSON parser call.
import json
raw = '{"title": "Loops"}'
data = json.____(raw)
print(data["title"])Answer: loads
Why: json.loads parses a JSON string into Python data.
Complete the exception raised by malformed JSON.
import json
try:
json.loads('{"title": "Loops",')
except json.____:
print("invalid json")Answer: JSONDecodeError
Why: json.loads raises JSONDecodeError when the text is not valid JSON.
Complete the missing-key condition.
import json
data = json.loads('{"title": "Loops"}')
required = ["title", "minutes"]
print([key for key in required if key ____ in data])Answer: not
Why: The comprehension keeps keys that are not present in the response.
Complete the dictionary iteration method.
required = {"title": str, "minutes": int}
data = {"title": "Loops", "minutes": "12"}
wrong = [key for key, kind in required.____() if not isinstance(data[key], kind)]
print(wrong)Answer: items
Why: items yields key-value pairs so each expected type can be checked.
What does this code print?
import json
raw = '{"title": "Loops", "level": "beginner"}'
data = json.loads(raw)
print(data["title"])Answer: Loops
Why: The title field is parsed as the string Loops.
What does the program output?
import json
raw = '{"title": "Loops"}'
data = json.loads(raw)
required = ["title", "minutes"]
print([key for key in required if key not in data])Answer: ['minutes']
Why: Only minutes is absent, so the list contains minutes.
What appears on the screen?
required = {"title": str, "minutes": int}
data = {"title": "Loops", "minutes": "12"}
print([key for key, kind in required.items() if not isinstance(data[key], kind)])Answer: ['minutes']
Why: minutes is a string, not an int, so it appears in the wrong-type list.
What is printed?
import json
try:
json.loads('{"title": "Loops",')
except json.JSONDecodeError:
print("invalid json")Answer: invalid json
Why: The trailing comma leaves the JSON object incomplete, so parsing fails safely.
Try the questions first
Open the practice page for Python prompt templates and structured output and answer the same 12 questions with instant feedback.