Bird
Raised Fist0
FastAPIframework~20 mins

Boolean query parameters in FastAPI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Boolean Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does FastAPI parse boolean query parameters?
Consider this FastAPI endpoint:
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(active: bool = Query(False)):
    return {"active": active}

What will be the output if you call /items/?active=true?
FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(active: bool = Query(False)):
    return {"active": active}
A{"active": "true"}
B{"active": true}
C{"active": false}
DHTTP 422 Unprocessable Entity error
Attempts:
2 left
💡 Hint
FastAPI automatically converts query parameters to the declared type.
📝 Syntax
intermediate
2:00remaining
Correct way to declare optional boolean query parameter with default
Which of the following is the correct way to declare an optional boolean query parameter named enabled with default True in FastAPI?
Aasync def endpoint(enabled: bool = Query(default=True)):
Basync def endpoint(enabled: Optional[bool] = Query(True)):
Casync def endpoint(enabled: bool = True):
Dasync def endpoint(enabled: bool = Query(True, alias='enabled')):
Attempts:
2 left
💡 Hint
Use Query to specify metadata and default values for query parameters.
🔧 Debug
advanced
2:00remaining
Why does this boolean query parameter always return False?
Given this FastAPI endpoint:
from fastapi import FastAPI

app = FastAPI()

@app.get("/check/")
async def check_flag(flag: bool = False):
    return {"flag": flag}

If you call /check/?flag=0 or /check/?flag=false, the response is always {"flag": false}. But calling /check/?flag=1 returns {"flag": true}. Why does flag=0 and flag=false both return False?
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/check/")
async def check_flag(flag: bool = False):
    return {"flag": flag}
AFastAPI raises an error for '0' but not for 'false'.
BFastAPI treats any string as True except empty strings.
CFastAPI treats '0' and 'false' as False boolean values by design.
DFastAPI ignores query parameters named 'flag'.
Attempts:
2 left
💡 Hint
Check how FastAPI converts common string values to booleans.
❓ state_output
advanced
2:00remaining
What is the output when boolean query parameter is missing?
Given this FastAPI endpoint:
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/status/")
async def status(online: bool = Query(...)):
    return {"online": online}

What happens if you call /status/ without the online query parameter?
FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/status/")
async def status(online: bool = Query(...)):
    return {"online": online}
AReturns HTTP 422 Unprocessable Entity error because parameter is required
BReturns {"online": false} because default is False
CReturns {"online": true} because default is True
DReturns {"online": null} because parameter is missing
Attempts:
2 left
💡 Hint
Query(...) means the parameter is required.
🧠 Conceptual
expert
3:00remaining
How does FastAPI handle boolean query parameters with multiple values?
Consider this FastAPI endpoint:
from fastapi import FastAPI, Query
from typing import List

app = FastAPI()

@app.get("/flags/")
async def flags(active: List[bool] = Query([])):
    return {"active": active}

What is the output of calling /flags/?active=true&active=false&active=1?
FastAPI
from fastapi import FastAPI, Query
from typing import List

app = FastAPI()

@app.get("/flags/")
async def flags(active: List[bool] = Query([])):
    return {"active": active}
A{"active": [true]}
B{"active": ["true", "false", "1"]}
CHTTP 422 Unprocessable Entity error
D{"active": [true, false, true]}
Attempts:
2 left
💡 Hint
FastAPI converts each query parameter value to the declared type in the list.

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

  1. 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.
  2. 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.
  3. Final Answer:

    A true or false flag passed in the URL to control API behavior -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    def read_items(active: Optional[bool] = Query(None)): -> Option B
  4. 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?
from fastapi import FastAPI, Query
from typing import Optional

app = FastAPI()

@app.get('/items')
async def read_items(active: Optional[bool] = Query(None)):
    return {"active": active}
medium
A. Error: invalid query parameter
B. {"active": "true"}
C. {"active": null}
D. {"active": true}

Solution

  1. Step 1: Understand FastAPI Boolean query parsing

    FastAPI automatically converts query strings like 'true' to Python boolean True.
  2. Step 2: Check the returned dictionary

    The function returns {"active": active}, so with active=true in URL, it returns {"active": true} as JSON.
  3. Final Answer:

    {"active": true} -> Option D
  4. Quick Check:

    Query 'true' converts to True boolean [OK]
Hint: FastAPI converts 'true' string to True boolean automatically [OK]
Common Mistakes:
  • Expecting string 'true' instead of boolean true
  • Assuming missing parameter returns false
  • Thinking it causes an error
4. Identify the error in this FastAPI endpoint that uses a Boolean query parameter:
from fastapi import FastAPI, Query

app = FastAPI()

@app.get('/items')
async def read_items(active: bool = Query()):
    return {"active": active}
medium
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

  1. Step 1: Check Query parameter usage

    Query() without a default value means the parameter is required. For optional, a default like None is needed.
  2. Step 2: Identify the missing default

    Since active is bool and no default is given, FastAPI expects the query parameter always present, else error.
  3. Final Answer:

    Query() requires a default value for optional parameters -> Option C
  4. 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

  1. Step 1: Analyze the required filtering behavior

    If available is None, return all items; else filter by matching available status.
  2. 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.
  3. 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
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard