Path & Query Parameters — Typed and Validated — Concept Check
Questions on Path & Query Parameters — Typed and Validated. Function parameters that appear in the path are path params; the rest become query params. Type hints convert and validate both; Path() and Query() add constraints like ge=1 or max_length=50.
Questions in this quiz
8 questions on Path & Query Parameters — Typed and Validated — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A FastAPI endpoint is defined with the route '/products/{product_id}'. Which parameter will be bound from the URL path?
- Aprice
- Bcategory
- Cavailability
- Dproduct_id
2.Given a FastAPI endpoint with a query parameter 'page: int = Query(default=1, ge=1)', what happens if the query string contains 'page=0'?
- AA 422 error is returned
- BThe endpoint returns an empty response
- CThe value is coerced to 1
- DThe default value 1 is used
3.You have an endpoint '/search' with a query parameter 'category: str = Query(max_length=10)'. What happens if 'category=electronics' is passed?
- AThe string is truncated
- BThe default value is used
- CThe request succeeds with a warning
- DA 422 error is returned
4.A developer defines an endpoint with 'confirmed: bool = False'. What values in the query string will set 'confirmed' to True?
- Aonly true
- Btrue, 1, yes
- Conly yes
- Dtrue, false, 1
5.An endpoint '/users/{user_id}' uses 'user_id: int'. What happens if the URL is '/users/abc'?
- AThe request succeeds with a warning
- BThe string 'abc' is used
- CThe endpoint returns an empty response
- DA 422 error is returned
6.A FastAPI endpoint has a query parameter 'tags: list[str]'. What does the query string 'tags=python&tags=fastapi' produce?
- AA single string 'python, fastapi'
- BAn empty list
- CA 422 error
- DA list ['python', 'fastapi']
7.What is the effect of defining a parameter with 'branch: Branch | None = None' where Branch is an Enum?
- ADefaults to the first Enum value
- BCauses a 422 error if missing
- CLimits 'branch' to specific Enum values
- DAllows any string value
8.In FastAPI, what is the role of Path() when used with a parameter in a function signature?
- ATo bind parameters from the query string
- BTo convert types automatically
- CTo make parameters optional
- DTo add validation and documentation