Answer key · 12 questions

Python prompt templates and structured output Exercises With Answers

All 12 questions for Python prompt templates and structured output in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

Which function safely parses a JSON model response?

  1. str.strip only
  2. json.loadscorrect
  3. exec
  4. eval

Answer: json.loads

Why: json.loads parses JSON data and rejects malformed syntax without executing code.

02 · Multiple choice

Does valid JSON guarantee the response has the required fields?

  1. Yes; every JSON object has title and minutes
  2. Only when the response is short
  3. Yes; JSON syntax proves the schema
  4. No; the schema and types still need validationcorrect

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.

03 · Multiple choice

What is a prompt template useful for?

  1. Keeping instructions stable while filling variable partscorrect
  2. Guaranteeing that the model is correct
  3. Encrypting the API key
  4. Removing the need to validate output

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.

04 · Multiple choice

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])
  1. []
  2. ['minutes']correct
  3. ['title', 'level']
  4. ['title']

Answer: ['minutes']

Why: The response has title and level, but it is missing the required minutes key.

05 · Fill in the blank

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.

06 · Fill in the blank

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.

07 · Fill in the blank

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.

08 · Fill in the blank

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.

09 · Predict output

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.

10 · Predict output

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.

11 · Predict output

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.

12 · Predict output

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.

Practice this lesson