Bird
Raised Fist0
FastAPIframework~20 mins

Required query parameters in FastAPI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FastAPI Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a required query parameter is missing?

Consider this FastAPI endpoint:

from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def read_item(q: str):
    return {"q": q}

What is the response if the client calls /items/ without the q parameter?

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def read_item(q: str):
    return {"q": q}
AThe server returns a 500 Internal Server Error.
BThe server returns an empty string for 'q' in the JSON response.
CThe server returns a 404 Not Found error.
DThe server returns a 422 Unprocessable Entity error because the required query parameter 'q' is missing.
Attempts:
2 left
💡 Hint

Think about what FastAPI does when a required parameter is not provided.

📝 Syntax
intermediate
2:00remaining
Which code correctly defines a required query parameter with a default value?

Which of the following FastAPI endpoint definitions correctly makes q a required query parameter?

Aasync def read_item(q: str):
Basync def read_item(q: str = None):
Casync def read_item(q: str = ""):
Dasync def read_item(q: Optional[str] = None):
Attempts:
2 left
💡 Hint

Required parameters do not have default values.

❓ state_output
advanced
2:00remaining
What is the output when a required query parameter is provided?

Given this FastAPI endpoint:

from fastapi import FastAPI
app = FastAPI()

@app.get("/search")
async def search(q: str):
    return {"query": q.upper()}

What is the JSON response when the client calls /search?q=fastapi?

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/search")
async def search(q: str):
    return {"query": q.upper()}
A{"query": "FASTAPI"}
B{"query": "fastapi"}
C422 Unprocessable Entity error
D{"query": null}
Attempts:
2 left
💡 Hint

Look at how the parameter q is used inside the function.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint raise a validation error?

Examine this code:

from fastapi import FastAPI
app = FastAPI()

@app.get("/items")
async def get_items(q: str = None):
    return {"q": q.upper()}

When calling /items without query parameters, what error occurs and why?

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items")
async def get_items(q: str = None):
    return {"q": q.upper()}
A422 Unprocessable Entity error because q is required but missing
BAttributeError because q is None and None has no method upper()
CNo error, returns {"q": ""}
DTypeError because q is not a string
Attempts:
2 left
💡 Hint

Consider what happens when q is None and you call upper().

🧠 Conceptual
expert
2:00remaining
How to enforce a required query parameter with a default value in FastAPI?

In FastAPI, you want a query parameter q that is required but also has a default value if not provided. Which statement is true?

AUse <code>q: str</code> with a validator to assign a default value.
BUse <code>q: str = "default"</code> to make it required with a default.
CYou cannot have a required query parameter with a default value; providing a default makes it optional.
DUse <code>q: str = Query(..., default="default")</code> to make it required with a default.
Attempts:
2 left
💡 Hint

Think about what 'required' means in FastAPI query parameters.

Practice

(1/5)
1. In FastAPI, how do you make a query parameter required so the client must provide it in the URL?
easy
A. Use Optional type hint
B. Set the parameter default to None
C. Use Query(...) in the function parameter
D. Use Body(...) instead of query

Solution

  1. Step 1: Understand FastAPI query parameters

    FastAPI uses function parameters to define query parameters automatically.
  2. Step 2: Use Query(...) to require parameter

    Using Query(...) tells FastAPI this parameter is required and must be in the URL.
  3. Final Answer:

    Use Query(...) in the function parameter -> Option C
  4. Quick Check:

    Required query parameter = Query(...) [OK]
Hint: Use Query(...) to make query parameters required [OK]
Common Mistakes:
  • Setting default to None makes parameter optional
  • Using Optional type hint allows missing parameter
  • Using Body(...) defines body, not query parameter
2. Which of the following is the correct syntax to declare a required query parameter named item_id of type int in FastAPI?
easy
A. def read_item(item_id: int = Query(None)):
B. def read_item(item_id: int = None):
C. def read_item(item_id: int = Body(...)):
D. def read_item(item_id: int = Query(...)):

Solution

  1. Step 1: Identify required query parameter syntax

    To make a query parameter required, use Query(...) as the default value.
  2. Step 2: Check each option

    def read_item(item_id: int = Query(...)): uses Query(...) correctly, making item_id required. def read_item(item_id: int = Query(None)): uses Query(None) which makes it optional. def read_item(item_id: int = None): uses default None without Query, optional. def read_item(item_id: int = Body(...)): uses Body(...) which makes it a required body field, not query parameter.
  3. Final Answer:

    def read_item(item_id: int = Query(...)): -> Option D
  4. Quick Check:

    Required query param = Query(...) [OK]
Hint: Use Query(...) as default to require query parameter [OK]
Common Mistakes:
  • Using Query(None) makes parameter optional
  • Using Body(...) for query parameter
  • Setting default None makes parameter optional
3. Given this FastAPI endpoint code:
from fastapi import FastAPI, Query
app = FastAPI()

@app.get("/items/")
async def read_items(q: str = Query(...)):
    return {"query": q}

What happens if a client calls /items/ without the q query parameter?
medium
A. Returns HTTP 422 error for missing required query parameter
B. Returns HTTP 404 not found error
C. Returns empty JSON {}
D. Returns JSON with {"query": null}

Solution

  1. Step 1: Understand Query(...) behavior

    Using Query(...) makes the parameter required. If missing, FastAPI returns an error automatically.
  2. Step 2: Check what error FastAPI returns

    FastAPI returns HTTP 422 Unprocessable Entity error when a required query parameter is missing.
  3. Final Answer:

    Returns HTTP 422 error for missing required query parameter -> Option A
  4. Quick Check:

    Missing required query param = HTTP 422 error [OK]
Hint: Missing required query param causes HTTP 422 error [OK]
Common Mistakes:
  • Expecting null or empty JSON instead of error
  • Confusing 404 not found with parameter error
  • Assuming optional parameter behavior
4. Identify the error in this FastAPI code snippet that tries to require a query parameter name:
from fastapi import FastAPI, Query
app = FastAPI()

@app.get("/users/")
async def get_user(name: str = Query(None)):
    return {"user": name}
medium
A. Missing type hint for name
B. The parameter is optional, not required
C. Query(...) should be replaced with Query(None)
D. Function should not be async

Solution

  1. Step 1: Analyze Query parameter default

    The code uses Query(None) which means the parameter is optional.
  2. Step 2: Check requirement for required parameter

    To make it required, it should use Query(...) instead of Query(None).
  3. Final Answer:

    The parameter is optional, not required -> Option B
  4. Quick Check:

    Query(None) = optional parameter [OK]
Hint: Query(None) means optional; use Query(...) for required [OK]
Common Mistakes:
  • Confusing Query(None) with required parameter
  • Ignoring type hints
  • Thinking async keyword affects parameter requirement
5. You want to create a FastAPI endpoint that requires two query parameters: category (string) and limit (integer). Both must be required. Which code snippet correctly enforces this?
hard
A. async def get_items(category: str = Query(...), limit: int = Query(...)):
B. async def get_items(category: str = None, limit: int = None):
C. async def get_items(category: str, limit: int = Query(None)):
D. async def get_items(category: str = Query(None), limit: int = Query(...)):

Solution

  1. Step 1: Understand required query parameters

    Both parameters must use Query(...) to be required.
  2. Step 2: Evaluate each option

    async def get_items(category: str = Query(...), limit: int = Query(...)): correctly uses Query(...) for both. async def get_items(category: str = None, limit: int = None) uses None defaults, making them optional. async def get_items(category: str, limit: int = Query(None)) makes only limit optional. async def get_items(category: str = Query(None), limit: int = Query(...)) makes category optional.
  3. Final Answer:

    async def get_items(category: str = Query(...), limit: int = Query(...)): -> Option A
  4. Quick Check:

    Both required = Query(...) for each [OK]
Hint: Use Query(...) for each required query parameter [OK]
Common Mistakes:
  • Setting default None makes parameter optional
  • Mixing required and optional unintentionally
  • Omitting Query makes parameter a path parameter
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