A boolean query parameter is a way to pass true or false values in the URL query string, which FastAPI automatically converts to Python's bool type.
Click to reveal answer
beginner
How does FastAPI interpret the query parameter ?active=true when declared as active: bool?
FastAPI converts the string "true" (case-insensitive) to the boolean value True. Similarly, "false" becomes False.
Click to reveal answer
intermediate
What happens if a boolean query parameter is not provided in the URL?
If the boolean query parameter is optional (e.g., active: bool = None), FastAPI sets it to None. If it is required without a default, FastAPI returns an error.
Click to reveal answer
beginner
How can you set a default value for a boolean query parameter in FastAPI?
You assign a default value in the function signature, like active: bool = False. This means if the parameter is missing, active will be False.
Click to reveal answer
beginner
Why is it important to use boolean query parameters carefully in URLs?
Because URLs are typed by users or generated by clients, using clear true/false values helps avoid confusion and ensures the API behaves as expected.
Click to reveal answer
In FastAPI, what value does the query parameter ?flag=false convert to if declared as flag: bool?
ATrue
BFalse
C"false" (string)
DNone
✗ Incorrect
FastAPI converts the string "false" (case-insensitive) to the boolean value False.
What happens if a required boolean query parameter is missing in the request URL?
AFastAPI raises a validation error
BFastAPI sets it to False automatically
CFastAPI sets it to None
DFastAPI ignores the parameter
✗ Incorrect
If a required boolean query parameter is missing, FastAPI raises a validation error because it expects a value.
How do you make a boolean query parameter optional in FastAPI?
AUse <code>flag: bool</code> without default
BUse <code>flag: Optional[str]</code>
CUse <code>flag: bool = None</code>
DUse <code>flag: int = 0</code>
✗ Incorrect
Setting flag: bool = None makes the parameter optional and allows it to be missing.
Which of these is a valid boolean query parameter value FastAPI recognizes as True?
A"true"
B"on"
C"1"
D"yes"
✗ Incorrect
FastAPI recognizes "true" (case-insensitive) as True. It also recognizes other strings like "yes", "1", or "on" (case-insensitive).
How can you provide a default value of True for a boolean query parameter named enabled?
A<code>enabled: bool</code>
B<code>enabled: bool = false</code>
C<code>enabled: Optional[bool]</code>
D<code>enabled: bool = True</code>
✗ Incorrect
Assigning enabled: bool = True sets the default value to True if the parameter is missing.
Explain how FastAPI handles boolean query parameters and how to make them optional or required.
Think about function parameter defaults and type conversion.
You got /3 concepts.
Describe best practices for using boolean query parameters in URLs to ensure clear API behavior.
Consider how users and clients read URLs.
You got /4 concepts.
Practice
(1/5)
1. What does a Boolean query parameter in FastAPI represent?
easy
A. A true or false flag passed in the URL to control API behavior
B. A number used to count API requests
C. A string that describes the API endpoint
D. A list of items sent in the request body
Solution
Step 1: Identify the purpose of Boolean query parameters
Boolean query parameters are flags that can be true or false, used to control how the API behaves.
Step 2: Identify the correct description
Only "A true or false flag passed in the URL to control API behavior" describes a true/false flag passed via URL query parameters.
Final Answer:
A true or false flag passed in the URL to control API behavior -> Option A
Quick Check:
Boolean query parameter = true/false flag [OK]
Hint: Boolean query parameters are true/false flags in the URL [OK]
Common Mistakes:
Confusing Boolean parameters with numbers or strings
Thinking Boolean parameters come from request body
Assuming Boolean parameters are lists
2. Which of the following is the correct way to declare an optional Boolean query parameter in FastAPI?
easy
A. def read_items(active: bool = Query(None)):
B. def read_items(active: Optional[bool] = Query(None)):
C. def read_items(active: str = Query(None)):
D. def read_items(active: int = Query(None)):
Solution
Step 1: Recall FastAPI syntax for optional Boolean query parameters
Use Optional[bool] with Query(None) to allow the parameter to be true, false, or missing.
Step 2: Match the correct function signature
def read_items(active: Optional[bool] = Query(None)): correctly uses Optional[bool] and Query(None) for an optional Boolean query parameter.
Final Answer:
def read_items(active: Optional[bool] = Query(None)): -> Option B
Quick Check:
Optional[bool] with Query(None) = correct syntax [OK]
Hint: Use Optional[bool] with Query(None) for optional Booleans [OK]
Common Mistakes:
Using bool without Optional for optional parameters
Using str or int instead of bool
Omitting Query import or default value
3. Given this FastAPI endpoint code, what will be the output if the URL is /items?active=true?
A. The return statement must convert active to string
B. Boolean query parameters cannot be used in FastAPI
C. Query() requires a default value for optional parameters
D. The function must be synchronous, not async
Solution
Step 1: Check Query parameter usage
Query() without a default value means the parameter is required. For optional, a default like None is needed.
Step 2: Identify the missing default
Since active is bool and no default is given, FastAPI expects the query parameter always present, else error.
Final Answer:
Query() requires a default value for optional parameters -> Option C
Quick Check:
Query() needs default for optional params [OK]
Hint: Use Query(None) for optional Boolean parameters [OK]
Common Mistakes:
Omitting default value for optional query parameters
Thinking async is not allowed
Trying to convert Boolean to string manually
5. You want to create a FastAPI endpoint that filters items by an optional Boolean query parameter available. If available is true, return only available items; if false, only unavailable items; if missing, return all items. Which code correctly implements this behavior?
hard
A. from fastapi import FastAPI, Query
from typing import Optional
app = FastAPI()
items = [{"name": "item1", "available": True}, {"name": "item2", "available": False}]
@app.get('/items')
async def read_items(available: Optional[bool] = Query(None)):
if available is None:
return items
return [item for item in items if item["available"] == available]
B. from fastapi import FastAPI
app = FastAPI()
@app.get('/items')
async def read_items(available: bool = False):
return {"available": available}
C. from fastapi import FastAPI, Query
app = FastAPI()
@app.get('/items')
async def read_items(available: bool = Query()):
return {"available": available}
D. from fastapi import FastAPI, Query
from typing import Optional
app = FastAPI()
@app.get('/items')
async def read_items(available: Optional[str] = Query(None)):
return {"available": available}
Solution
Step 1: Analyze the required filtering behavior
If available is None, return all items; else filter by matching available status.
Step 2: Check code correctness
from fastapi import FastAPI, Query
from typing import Optional
app = FastAPI()
items = [{"name": "item1", "available": True}, {"name": "item2", "available": False}]
@app.get('/items')
async def read_items(available: Optional[bool] = Query(None)):
if available is None:
return items
return [item for item in items if item["available"] == available] uses Optional[bool] with Query(None) and filters items correctly based on available value.
Final Answer:
from fastapi import FastAPI, Query
from typing import Optional
app = FastAPI()
items = [{"name": "item1", "available": True}, {"name": "item2", "available": False}]
@app.get('/items')
async def read_items(available: Optional[bool] = Query(None)):
if available is None:
return items
return [item for item in items if item["available"] == available] -> Option A
Quick Check:
Optional[bool] with filtering logic = correct [OK]
Hint: Use Optional[bool] and check None to handle missing query param [OK]
Common Mistakes:
Using bool without Optional for optional parameter
Not filtering items based on Boolean value
Using string type instead of Boolean
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.