Answer key · 12 questions

Python Type Hints Exercises With Answers

All 12 questions for Python Type Hints 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 Type Hints, 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 does name: str mean in a function signature?

  1. Name is optional
  2. Name is converted to a string
  3. Name must be a class
  4. Name is expected to be a stringcorrect

Answer: Name is expected to be a string

Why: The annotation documents the expected type; it does not convert the value.

02 · Multiple choice

What does -> int describe?

  1. The first parameter
  2. A runtime check
  3. A required import
  4. The return typecorrect

Answer: The return type

Why: The arrow introduces the function's return annotation.

03 · Multiple choice

What does list[int] mean?

  1. An integer that is also a list
  2. A list of exactly one item
  3. A list whose items are integerscorrect
  4. A list with one integer

Answer: A list whose items are integers

Why: The type inside the brackets describes each element.

04 · Multiple choice

What does float | None mean?

  1. The function returns two floats
  2. The value is always zero
  3. The value must be a tuple
  4. The value may be a float or Nonecorrect

Answer: The value may be a float or None

Why: The union lists both possible kinds of value.

05 · Fill in the blank

Complete the parameter annotation.

def greet(name: ____) -> str:
    return f"Hello, {name}"

Answer: str

Why: The name should be a string.

06 · Fill in the blank

Complete the list element type.

def total(values: list[____]) -> int:
    return sum(values)

Answer: int

Why: values contains integers.

07 · Fill in the blank

Complete the union operator.

def find_price(item: str) -> float ____ None:
    return None

Answer: |

Why: A vertical bar combines float and None.

08 · Fill in the blank

Complete the return annotation arrow.

def double(n: int) ____ int:
    return n * 2

Answer: ->

Why: The arrow separates the parameter list from the return type.

09 · Predict output

What appears on the screen?

def f(n: int) -> int:
    return n + 1
print(f(2))

Answer: 3

Why: The hint describes the value, and the function still returns three.

10 · Predict output

What is printed?

def f(x: str = "a") -> str:
    return x * 2
print(f())

Answer: aa

Why: The default is a, and a repeated twice is aa.

11 · Predict output

What is the output?

def f(x: int | None) -> str:
    return "none" if x is None else str(x)
print(f(None))

Answer: none

Why: None takes the first branch and returns the string none.

12 · Predict output

What does this code print?

from typing import get_type_hints
def f(n: int) -> str:
    return str(n)
print(get_type_hints(f)["n"].__name__)

Answer: int

Why: get_type_hints reads the annotation, whose type object is named int.

Try the questions first

Open the practice page for Python Type Hints and answer the same 12 questions with instant feedback.

Practice this lesson