programming
beginner
10 sample questions
Apis MCQ Practice Test
Consuming RESTful APIs to fetch data.
Building client-server architecture with JSON responses.
Q1. When using the Fetch API to send a POST request with JSON data, which header should be included to indicate that the request body contains JSON data?
-
A. application/x-www-form-urlencoded
-
B. application/json ✓
-
C. text/plain
-
D. text/html
Explanation: The Fetch API requires the Content-Type header to be set to application/json when sending JSON data in the request body. This informs the server that the request body contains JSON data and should be parsed accordingly.
Q2. What is the primary difference between the 'GET' and 'HEAD' HTTP methods when interacting with a RESTful API?
-
A. GET requests return the full response body, while HEAD requests return only the headers. ✓
-
B. HEAD requests are used for caching, whereas GET requests are used for retrieving data.
-
C. GET requests are used for retrieving data, while HEAD requests are used for updating resources.
-
D. GET requests are idempotent, whereas HEAD requests are not.
Explanation: When interacting with a RESTful API, the primary difference between the 'GET' and 'HEAD' HTTP methods lies in their response bodies. 'GET' requests return the full response body, including the content, whereas 'HEAD' requests return only the headers of the requested resource.
Q3. In an ASP.NET Web API, what is the purpose of the "[ApiController]" attribute?
-
A. To enable CORS support for the API
-
B. To automatically apply the "[Produces("application/json")]" attribute to all controllers
-
C. To indicate that the controller handles only HTTP POST requests
-
D. To enable model validation and automatic handling of validation errors ✓
Explanation: The [ApiController] attribute in ASP.NET Web API enables several features: automatic model validation (returning HTTP 400 for invalid models), inference of binding sources ([FromBody] for complex types), inference of [ApiController] for attribute routing, and multipart/form-data content inference. It does NOT automatically apply [Produces("application/json")].
Q4. What is the primary purpose of the "Accept" header in a RESTful API request?
-
A. To specify the data format of the response ✓
-
B. To authenticate the user
-
C. To indicate the type of HTTP method used
-
D. To specify the caching behavior
Explanation: The "Accept" header is used to inform the server about the data format the client can handle, allowing it to return the response in the most suitable format.
Q5. What is the primary difference between the 'GET' and 'HEAD' HTTP methods in the context of API development?
-
A. The 'GET' method sends data while 'HEAD' does not.
-
B. The 'GET' method is used for updating resources while 'HEAD' is used for creating resources.
-
C. The 'GET' method is used for retrieving data while 'HEAD' is used for retrieving metadata. ✓
-
D. The 'GET' method is used for creating resources while 'HEAD' is used for updating resources.
Explanation: In API development, 'GET' is used to retrieve data from a resource, while 'HEAD' is used to retrieve metadata about the resource, such as its existence, location, or last modified date.
Q6. What is the primary purpose of using the 'Content-Type: application/json' header in a RESTful API request?
-
A. To indicate the request body contains form data
-
B. To specify the data format of the request body ✓
-
C. To authenticate the user making the request
-
D. To indicate the request body contains XML data
Explanation: The 'Content-Type: application/json' header is used to inform the server that the request body contains JSON data, allowing the server to parse and process it accordingly.
Q7. Which HTTP method is used to update a resource in a RESTful API, but also allows partial updates?
-
A. PUT
-
B. PATCH ✓
-
C. DELETE
-
D. POST
Explanation: The PATCH method is used to update a resource in a RESTful API, but it allows partial updates, meaning that only the fields that need to be updated are sent in the request body, whereas PUT would require the entire resource to be sent.
Q8. What is the primary purpose of the "Content-Disposition" header in an HTTP response when using the Fetch API to download a file?
-
A. To specify the file name of the downloaded file ✓
-
B. To set the caching behavior of the response
-
C. To indicate the type of content returned in the response
-
D. To specify the encoding of the response body
Explanation: The "Content-Disposition" header is used to specify the file name of the downloaded file when the Fetch API is used to fetch a file. This allows the browser to save the file with the specified name.
Q9. What is the purpose of the "Authorization" header in a RESTful API request?
-
A. To specify the request method
-
B. To authenticate and authorize the user ✓
-
C. To cache the response
-
D. To specify the request body
Explanation: The "Authorization" header is used to authenticate and authorize the user in a RESTful API request. It typically contains credentials such as a token or username and password to verify the user's identity and ensure they have permission to access the requested resource.
Q10. In a RESTful API, what is the primary purpose of the "Accept" request header when making a request to a resource?
-
A. To specify the format of the response ✓
-
B. To indicate the type of request
-
C. To authenticate the user
-
D. To specify the caching policy
Explanation: The "Accept" request header is used to specify the format of the response, such as JSON or XML. This allows the client to request a specific format and the server to respond accordingly.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free