0
0
FastAPIframework~20 mins

Pydantic model definition in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pydantic Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Pydantic model validation?
Given the Pydantic model and input data below, what will be the value of user.age after validation?
FastAPI
from pydantic import BaseModel, Field

class User(BaseModel):
    name: str
    age: int = Field(default=18, ge=0, le=120)

user = User(name='Alice', age=25)
ARaises a ValidationError
B18
CNone
D25
Attempts:
2 left
💡 Hint
Check the value passed to the model and the default value.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Pydantic model with an optional email field?
Select the code snippet that correctly defines a Pydantic model with an optional email field of type str.
A
from pydantic import BaseModel

class User(BaseModel):
    email: str = Optional[None]
B
from pydantic import BaseModel

class User(BaseModel):
    email: str | None = None
C
from pydantic import BaseModel
from typing import Optional

class User(BaseModel):
    email: Optional[str] = None
D
from pydantic import BaseModel
from typing import Optional

class User(BaseModel):
    email: str = Optional[str]
Attempts:
2 left
💡 Hint
Use the correct typing for optional fields and default values.
🔧 Debug
advanced
2:00remaining
What error does this Pydantic model raise when instantiated?
Consider this model and instantiation code. What error will occur?
FastAPI
from pydantic import BaseModel

class Product(BaseModel):
    id: int
    price: float

product = Product(id='abc', price=19.99)
ATypeError: id must be int
BValidationError: id must be an integer
CNo error, product created successfully
DValueError: price must be float
Attempts:
2 left
💡 Hint
Check the type of the id field and the value passed.
state_output
advanced
2:00remaining
What is the output of this Pydantic model with a computed property?
Given the model below, what will item.total_price output?
FastAPI
from pydantic import BaseModel

class Item(BaseModel):
    name: str
    quantity: int
    price_per_unit: float

    @property
    def total_price(self) -> float:
        return self.quantity * self.price_per_unit

item = Item(name='Pen', quantity=3, price_per_unit=1.5)
print(item.total_price)
A4.5
B3
C1.5
DRaises AttributeError
Attempts:
2 left
💡 Hint
Multiply quantity by price_per_unit.
🧠 Conceptual
expert
3:00remaining
Which option best describes Pydantic's model validation behavior on nested models?
Consider a Pydantic model with a nested model field. Which statement is true about how Pydantic validates nested models?
APydantic validates nested models recursively, applying all field validations in nested models automatically.
BPydantic only validates the top-level model and ignores nested models unless explicitly validated.
CValidation of nested models requires manual calls to their <code>validate()</code> method.
DNested models are treated as plain dictionaries without validation.
Attempts:
2 left
💡 Hint
Think about how Pydantic handles complex data structures.