QuizzesType Hints — Optional, Union, Generics & mypy — Concept Check
intermediate
python

Type Hints — Optional, Union, Generics & mypy — Concept Check

Questions on Type Hints — Optional, Union, Generics & mypy. Type hints document intent and let tools (mypy, IDEs, FastAPI, Pydantic) catch bugs before runtime — Python stays dynamic, the annotations stay optional but professional.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on Type Hints — Optional, Union, Generics & mypy — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.A developer uses the following function signature: `def process(data: list[int] | None) -> int:`. What must they do before using `data`?

    • ACheck if `data` is not None
    • BAssume `data` is always a list
    • CDirectly iterate over `data`
    • DUse `data` without any checks
  2. 2.You have a function `def calculate_total(prices: list[float]) -> float:`. What type hint would you use to indicate this function might not return a value?

    • AOptional[float]
    • Bfloat | None
    • Cfloat
    • DUnion[float, None]
  3. 3.A function `def get_user_data(user_id: int) -> dict[str, str] | None:` is called, but the result is used without a check. What error might mypy report?

    • ANameError: 'None' is not defined
    • BValueError: Invalid return type
    • CTypeError: 'NoneType' object is not subscriptable
    • DSyntaxError: Invalid type hint
  4. 4.Given `T = TypeVar('T')`, what does `def first(items: list[T]) -> T | None:` ensure?

    • AThe function returns the same type as the list elements
    • BThe function only accepts integer lists
    • CThe function always returns a list
    • DThe function returns a fixed type
  5. 5.A developer writes `def add(x: int, y: int) -> int:` but uses `add(1.5, 2.5)`. What will mypy likely report?

    • ARuntime error: Type mismatch
    • BType error: Argument types do not match
    • CSyntax error: Invalid function call
    • DNo error, as Python is dynamic
  6. 6.Why might you use `Callable[[int], int]` in a type hint?

    • ATo enforce a function returns a string
    • BTo allow any type of function
    • CTo specify a function that takes and returns an int
    • DTo ensure a function takes no arguments
  7. 7.What is the purpose of using `Generic[T]` in a class definition?

    • ATo restrict the class to a single type
    • BTo make the class only work with integers
    • CTo enable the class to handle multiple types
    • DTo simplify type annotations
  8. 8.What is a key benefit of using type hints in Python functions?

    • AThey help tools catch bugs before runtime
    • BThey make the code run faster
    • CThey are required for all Python functions
    • DThey change the function's behavior at runtime