FastAPI Fundamentals: Modern Python API Quiz Quiz

Explore core concepts of building modern web APIs with FastAPI, including routing, request handling, data validation, and asynchronous processing. This quiz is designed to improve your foundational knowledge of FastAPI and how it simplifies API development using Python.

  1. Basic Usage

    Which decorator is typically used to define a GET endpoint in FastAPI?

    1. @route.get
    2. @app.get
    3. @api_get
    4. @get.app

    Explanation: The correct decorator for defining a GET endpoint in FastAPI is @app.get. This links the function directly to the GET HTTP method at a specified path. '@route.get', '@api_get', and '@get.app' are not valid FastAPI decorators and would either cause errors or be ignored.

  2. App Initialization

    What is the correct way to create a FastAPI application instance?

    1. app = APIFast()
    2. app = CreateFastAPI()
    3. app = fastapi.App()
    4. app = FastAPI()

    Explanation: You should create an instance using 'app = FastAPI()' to initialize a new FastAPI application. 'fastapi.App()', 'APIFast()', and 'CreateFastAPI()' are incorrect usages and will result in errors because these class or function names do not exist in FastAPI.

  3. Path Parameters

    How do you declare a dynamic path parameter in a FastAPI route?

    1. /items/[item_id]
    2. /items/item_id
    3. /items/u003Citem_idu003E
    4. /items/{item_id}

    Explanation: Dynamic path parameters in FastAPI are defined using curly braces, like '/items/{item_id}'. Using square brackets, angle brackets, or leaving out the braces won't correctly identify path parameters for the framework. These alternatives are used by other frameworks but not in FastAPI.

  4. Request Body

    Which Python feature is most commonly used by FastAPI to define request body data structures?

    1. Dictionaries
    2. Pydantic models
    3. Named tuples
    4. Classic classes

    Explanation: FastAPI leverages Pydantic models to handle request body validation and data parsing due to their ease of use and built-in validation. While dictionaries and named tuples can hold data, they do not provide automatic validation or type hints. Standard Python classes lack integrated data validation as well.

  5. Response Model

    What is the main purpose of the 'response_model' parameter in a FastAPI endpoint?

    1. To choose the HTTP status code
    2. To set the allowed request methods
    3. To specify the output data schema
    4. To define database connections

    Explanation: 'response_model' is used to define which data schema the response should adhere to, ensuring consistency and better documentation. It does not control status codes or request methods, nor does it relate to database connectivity. The other options relate to different parts of FastAPI configuration.

  6. Validation

    If a client sends invalid data to a FastAPI endpoint that uses a Pydantic model, what kind of response is automatically generated?

    1. An HTML 500 error page
    2. A redirect to the homepage
    3. A JSON error response with status 422
    4. A plain text 404 error

    Explanation: FastAPI returns a detailed JSON error message with a 422 Unprocessable Entity status when input data doesn't match the Pydantic model. A 404 error indicates a missing resource, not a validation issue. A 500 error would suggest a server bug, and a redirect is not the default behavior for input validation failures.

  7. Query Parameters

    How are optional query parameters typically declared in FastAPI endpoint functions?

    1. By omitting the type hint
    2. By using the 'QuerySet' class
    3. By providing a default value, like 'value: int = None'
    4. By prefixing with '?' in the route path

    Explanation: Optional query parameters are set by assigning default values, such as 'value: int = None', which makes them optional for requests. Omitting type hints does not make a parameter optional. Adding a question mark in the path is not a valid FastAPI syntax. 'QuerySet' is unrelated to FastAPI’s query parameter handling.

  8. Running the Server

    Which command is commonly used to run a FastAPI application in development?

    1. uvicorn main:app
    2. runfastapi main
    3. python runserver.py
    4. fastapi start

    Explanation: The standard command is 'uvicorn main:app', where 'main' is the Python file and 'app' is the FastAPI instance. 'python runserver.py' and 'fastapi start' are not valid FastAPI commands, and 'runfastapi main' does not execute a FastAPI application correctly.

  9. Asynchronous Endpoints

    How do you declare an asynchronous endpoint in FastAPI?

    1. By placing 'await' after the return statement
    2. By decorating the function with '@async.handler'
    3. By adding 'async' before the function definition
    4. By naming the function with a '_async' suffix

    Explanation: Declaring the function with 'async def' makes the endpoint run asynchronously, enabling non-blocking I/O. Naming conventions and adding suffixes do not affect asynchronous behavior. Using 'await' after return or decorators like '@async.handler' have no effect in FastAPI.

  10. Automatic Documentation

    Which feature does FastAPI provide by default for API documentation and interactive testing?

    1. Built-in email notifications
    2. An auto-generated interactive documentation UI
    3. File system monitoring tools
    4. Automatic database migration

    Explanation: FastAPI automatically generates an interactive UI for API documentation and endpoint testing, making it easy to visualize and test your API. Database migrations, email notifications, and file monitoring tools are not provided out of the box by FastAPI. The other options are unrelated to documentation features.