Flutter Networking: REST APIs u0026 JSON Handling Quiz Quiz

Enhance your understanding of Flutter networking concepts, focusing on REST API integration and JSON data handling. This quiz covers HTTP requests, response parsing, error management, and efficient data processing for building robust cross-platform applications.

  1. Making HTTP Requests

    Which HTTP method should you use to submit new data to a REST API endpoint when creating a resource in Flutter?

    1. POST
    2. FETCH
    3. GET
    4. PUT

    Explanation: POST is the recommended method for creating new resources in REST API architectures. GET retrieves data, PUT is generally used for updating existing resources, and FETCH is not a standard HTTP method. Using any method other than POST for creating resources may result in errors or unexpected behavior in RESTful systems.

  2. Parsing JSON Responses

    After receiving a JSON response as a string in Flutter, what is the most appropriate function to convert this string into a usable Dart object?

    1. toJson
    2. parseJson
    3. jsonConvert
    4. jsonDecode

    Explanation: jsonDecode is the correct function to convert a JSON string into a Dart object. parseJson and jsonConvert are not standard functions and would result in errors if called directly. toJson is used for the opposite process—converting a Dart object to a JSON string.

  3. Handling Asynchronous HTTP Calls

    What Flutter language feature should you use to wait for an HTTP response without blocking the UI thread?

    1. async/await
    2. defer
    3. suspend
    4. yield

    Explanation: The async/await keywords are used in Flutter to perform asynchronous operations, such as network calls, without freezing the user interface. suspend is used in another ecosystem, defer is unrelated to asynchronous code in Dart, and yield is typically used in generator functions, not for awaiting results.

  4. Decoding JSON Arrays

    When a REST API returns a JSON array of objects, which Dart data type is most suitable for holding the decoded list?

    1. Mapu003CString, dynamicu003E
    2. String
    3. Setu003Cintu003E
    4. Listu003Cdynamicu003E

    Explanation: Listu003Cdynamicu003E is appropriate for storing a list of objects parsed from a JSON array. Mapu003CString, dynamicu003E is suitable for single JSON objects, String does not represent object collections, and Setu003Cintu003E is a collection type restricted to integers, not generic objects.

  5. Error Handling in Network Calls

    If an API call in Flutter returns a status code 404, what is the most accurate description of this response?

    1. Server overloaded
    2. Invalid input
    3. Resource not found
    4. Success

    Explanation: A 404 status code indicates that the requested resource could not be found on the server. Success is usually denoted by 200 status codes, invalid input results in 400-series errors but not specifically 404, and server overload would result in 500-series status codes.

  6. Custom Headers in Requests

    Which component of an HTTP request allows you to send extra information, such as authentication tokens, from a Flutter app to a REST API?

    1. Parameters
    2. Body
    3. Footer
    4. Headers

    Explanation: HTTP headers are used to send additional information, including authentication tokens, in network requests. The body contains the main content of the request (like data to be created), parameters adjust the query or path, and 'Footer' is not part of HTTP terminology.

  7. Serializing Model Classes

    What method is commonly implemented in Dart model classes to convert an object into a map suitable for JSON encoding?

    1. fromJson
    2. toJson
    3. convert
    4. serialize

    Explanation: The toJson method is conventionally implemented in Dart model classes for converting objects into a format ready for JSON encoding. fromJson serves the opposite purpose, deserialize and convert are not Dart conventions for this process.

  8. Deserializing JSON to Models

    Which method should a Dart model class implement to create an instance from a map decoded from JSON?

    1. fromJson
    2. mapToClass
    3. decodeJson
    4. toJson

    Explanation: Implementing fromJson in a Dart model class is standard for initializing an object from a decoded map. toJson is for converting objects into maps, while mapToClass and decodeJson are not recognized Dart naming conventions.

  9. Handling Malformed JSON

    If you attempt to decode a malformatted JSON string in Flutter, what type of error will typically occur?

    1. FormatException
    2. WrongTypeException
    3. TimeoutException
    4. NullPointerException

    Explanation: A FormatException is thrown when Dart fails to decode a badly-formed JSON string. TimeoutException relates to timeouts, NullPointerException is not a Dart error type, and WrongTypeException is not a common Dart error for JSON issues.

  10. Testing REST API Endpoints

    Which HTTP status code returned by an endpoint typically indicates a successful GET request?

    1. 500
    2. 200
    3. 400
    4. 302

    Explanation: A 200 status code signals success for a GET request. 400 indicates a client error such as bad request, 302 indicates a redirection, and 500 means the server encountered an internal error.

  11. Updating Existing Data

    Which HTTP method is most appropriate for updating an entire resource on a REST API server from a Flutter app?

    1. PUT
    2. POST
    3. PATCH
    4. FETCH

    Explanation: PUT is designed for replacing an existing resource in its entirety on a server. PATCH modifies only specified data, POST creates a new resource, and FETCH is not a valid HTTP method.

  12. Efficiently Managing Large JSON Responses

    What technique helps prevent app freezes when processing large JSON data received via Flutter’s networking features?

    1. Using compute to parse off the main thread
    2. Using delay before parsing
    3. Reducing screen brightness
    4. Increasing the device's memory

    Explanation: Using compute allows parsing of large JSON data in a separate isolate, keeping the main user interface responsive. Increasing memory is not a software solution, using delay simply postpones the problem, and screen brightness has no effect on JSON parsing.

  13. Including Parameters in URLs

    When sending a GET request in Flutter and adding dynamic search criteria, how should you include these values?

    1. In the request body
    2. As query parameters in the URL
    3. In HTTP headers
    4. In response handlers

    Explanation: Query parameters are the correct way to include search criteria in GET requests. Sending them in the request body is not supported for GET, HTTP headers are meant for control information, and response handlers do not affect outbound requests.

  14. Identifying Connection Timeouts

    If your Flutter app tries to reach a REST API but gets no response for a long time, which exception will most likely be thrown?

    1. SocketException
    2. RedirectException
    3. TimeoutException
    4. FormatException

    Explanation: TimeoutException is thrown when a network request takes longer than allowed. FormatException relates to data formatting, SocketException happens upon general network failures, and RedirectException is not a standard error in this context.

  15. Choosing the Right Content Type

    When sending JSON data to a REST API in Flutter, what 'Content-Type' should you specify in the HTTP headers?

    1. text/html
    2. image/png
    3. application/json
    4. application/xml

    Explanation: application/json correctly specifies the media type for JSON data. text/html is for HTML content, application/xml is for XML, and image/png is used only for PNG images; using the wrong type can lead to request failures.

  16. Managing API Rate Limits

    What is a common best practice if an API temporarily limits the number of requests your Flutter app can send?

    1. Implement request throttling
    2. Increase device CPU speed
    3. Disable JSON decoding
    4. Ignore error responses

    Explanation: Request throttling is used to ensure your app stays within allowed API limits. Increasing CPU speed has no effect on server limits, ignoring error responses does not address the issue, and disabling JSON decoding is unrelated.