AlgoMaster Logo

Type Hints & Annotations - Quiz

Last Updated: December 6, 2025

Type Hints & Annotations Exercises

31 quizzes

1
Code Completion

Complete the code to annotate the age parameter as an integer and call the function

python
1
info: str = describe_user(age25)

Click an option to fill the blank:

2
Code Completion

Complete the code so the variable user_id is explicitly annotated as an integer

python
1
user_idint = 42

Click an option to fill the blank:

3
Code Completion

Complete the code to declare that the function returns a string

python
1
def greet_user(name: str)str:

Click an option to fill the blank:

4
Multiple Choice

What do type hints change about how Python executes code?

5
Multiple Choice

Which is a correct function annotation for a function that takes a float and returns an int?

6
Multiple Choice

Which type from the typing module lets a parameter accept either an int or a str?

7
Multiple Choice

Which import is needed to use List and Dict for type hints in Python 3.9 code that prefers typing module generics?

8
Multiple Choice

What is the main benefit of using a generic class like Generic[T]?

9
Multiple Choice

In modern Python, which is the most concise way to say a value can be int or None?

10
Multiple Choice

Which type annotation best describes a callback that takes a str and returns bool?

11
Multiple Choice

How do you define a simple type alias for Dict[str, int]?

12
Multiple Choice

Which command runs mypy on a file called app.py?

13
Multiple Choice

Given price: float | None, which check is safest before using price in arithmetic?

14
Multiple Choice

What does mypy do when it finds a type mismatch?

15
Sequencing

Order the steps to create a generic Box class that stores any type using the typing module.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to use mypy to check a typed function in a new file.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code using a variable annotation?

1total: int = 3
2extra: int = 4
3print(total + extra)
18
Output Prediction

What is the single line of output of this code with Optional type?

1from typing import Optional
2
3def format_price(price: Optional[float]) -> str:
4    if price is None:
5        return "N/A"
6    return f"${price:.2f}"
7
8print(format_price(None))
19
Output Prediction

What is printed when using a Union type in this function?

1from typing import Union
2
3def stringify(value: Union[int, float]) -> str:
4    return str(value * 2)
5
6print(stringify(2.5))
20
Output Prediction

What is the output of this code using a Callable type?

1from typing import Callable
2
3Processor = Callable[[str], str]
4
5def shout(text: str) -> str:
6    return text.upper()
7
8def run(p: Processor, value: str) -> str:
9    return p(value) + "!"
10
11print(run(shout, "hi"))
21
Output Prediction

What is the output of this code using a generic Box?

1from typing import Generic, TypeVar
2
3T = TypeVar('T')
4
5class Box(Generic[T]):
6    def __init__(self, item: T) -> None:
7        self.item = item
8
9    def get_item(self) -> T:
10        return self.item
11
12name_box: Box[str] = Box("Alice")
13print(name_box.get_item())
22
Bug Spotting

Find the type-related bug in this code that uses Optional and Union.

Click on the line(s) that contain the bug.

python
1
from typing import Optional, Union
2
 
3
def parse_age(value: Optional[str]) -> Union[int, None]:
4
    if value is None:
5
        return "unknown"
6
    return int(value)
23
Bug Spotting

Identify the bug that would be reported by mypy in this generic Stack implementation.

Click on the line(s) that contain the bug.

python
1
from typing import Generic, List, TypeVar
2
 
3
T = TypeVar('T')
4
 
5
class Stack(Generic[T]):
6
    def __init__(self) -> None:
7
        self.items: List[T] = []
8
 
9
    def push(self, item: T) -> None:
10
        self.items.append(item)
11
 
12
    def pop(self) -> T:
13
        return self.items.pop() if self.items else None
24
Matching

Match each typing construct with its most accurate description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match the mypy behavior with the situation.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to define a type alias for a list of user IDs and annotate the parameter correctly.

python
1
from typing import List
2
3
UserIdList =
4
5
def active_user_count(user_ids: ) -> int:
6
return len(user_ids)

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code so that the function can accept either an int or a float and returns a str.

python
1
from typing import
2
3
def to_label(value: ) -> str:
4
return f"Value: {value}"

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to declare a generic Box class that stores items of any type.

python
1
from typing import ,
2
3
T = ('T')
4
5
class Box():
6
def __init__(self, item: T) -> None:
7
self.item = item

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code so that the function parameter may be a str or None and mypy knows when it's safe to call .upper().

python
1
from typing import
2
3
def normalize_name(name: ) -> str:
4
if name is None:
5
return ""
6
return name.()

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that would cause a mypy error because of an incompatible assignment.

Click on the line to select.

python
1
from typing import List
2
 
3
numbers: List[int] = [1, 2, 3]
4
name: str = "Alice"
5
name = 5
6
print(name)
31
Hotspot Selection

Click the line where the return type does not match the annotation.

Click on the line to select.

python
1
from typing import Optional
2
 
3
def first_letter(text: str) -> Optional[str]:
4
    if not text:
5
        return None
6
    return text

Premium Content

Subscribe to unlock full access to this content and more premium articles.