Answer key · 12 questions

Python Testing with assert Exercises With Answers

All 12 questions for Python Testing with assert 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 Testing with assert, 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

What happens when an assert condition is false?

  1. Python exits with code 0
  2. The condition is skipped
  3. AssertionError is raisedcorrect
  4. The program prints False

Answer: AssertionError is raised

Why: A failed assert raises AssertionError.

02 · Multiple choice

What is the part after the comma in assert x == 3, 'x should be 3'?

  1. A second condition
  2. The expected type
  3. A comment only
  4. A message attached to the failurecorrect

Answer: A message attached to the failure

Why: The second expression becomes the AssertionError message.

03 · Multiple choice

What does running Python with -O do to assert statements?

  1. Makes them raise TypeError
  2. Removes themcorrect
  3. Runs them twice
  4. Turns them into print calls

Answer: Removes them

Why: The -O flag strips assert statements, so do not use them for security checks.

04 · Multiple choice

Where is assert appropriate?

  1. For user-facing error messages
  2. In tests and internal invariantscorrect
  3. For validating a password form
  4. For handling network errors

Answer: In tests and internal invariants

Why: Assert documents assumptions; it is not a user-input validation mechanism.

05 · Fill in the blank

Complete the statement that checks the sum.

total = 2 + 2
____ total == 4
print("ok")

Answer: assert

Why: The keyword assert begins the check.

06 · Fill in the blank

Complete the expected value in the message.

total = 2 + 2
assert total == 4, "total should be ____"

Answer: 6

Why: The message should state the expected total.

07 · Fill in the blank

Complete the exception type.

try:
    assert False
except ____ as error:
    print("caught")

Answer: AssertionError

Why: A failed assert raises AssertionError.

08 · Fill in the blank

Complete the expected square.

def square(n):
    return n * n
assert square(3) == ____
print("ok")

Answer: 9

Why: Three squared is nine.

09 · Predict output

What is printed?

assert 2 + 2 == 4
print("ok")

Answer: ok

Why: The assertion passes, so the next line runs.

10 · Predict output

What is the output?

def f(n):
    return n * 2
assert f(3) == 6
print(f(3))

Answer: 6

Why: The function returns six, and the assertion passes.

11 · Predict output

What does this code print?

try:
    assert False, "bad"
except AssertionError as error:
    print(error)

Answer: bad

Why: The message after the comma becomes the exception text.

12 · Predict output

What does the program output?

assert isinstance("x", str)
print("str")

Answer: str

Why: The string passes the isinstance check, so str is printed.

Try the questions first

Open the practice page for Python Testing with assert and answer the same 12 questions with instant feedback.

Practice this lesson