programming
intermediate
10 sample questions
Api Design MCQ Practice Test
Principles and best practices for creating effective and secure application programming interfaces.
Q1. You're designing a RESTful API for a social media platform. To allow users to retrieve a paginated list of their friend's posts, including the ability to filter by post type (photo, video, text) and sort by date (asc/desc), which HTTP method and parameterization strategy best balances REST principles with efficient data retrieval, minimizing round trips and potential ambiguity? Assume post type and sort order are optional parameters.
-
A. GET /users/{user_id}/friends/posts?type={post_type}&sort={sort_order}&page={page_number}&limit={page_size} ✓
-
B. POST /users/{user_id}/friends/posts/search with JSON body containing type, sort, page, limit
-
C. GET /users/{user_id}/friends/posts/{post_type}?sort={sort_order}&page={page_number}&limit={page_size} (separate requests for each type)
-
D. PUT /users/{user_id}/friends/posts/query with JSON body containing type, sort, page, limit
Explanation: This option best adheres to RESTful principles. GET is appropriate for retrieval. Query parameters handle optional filtering and pagination cleanly, avoiding the need for multiple requests or the less RESTful approach of using POST for data retrieval. POST and PUT are typically used for creating and updating resources, respectively.
Q2. You're designing a RESTful API for a social media platform. Users can create posts containing images. To optimize for performance and reduce redundancy, which approach is BEST for handling image uploads and retrieval within the API, considering potential scalability issues and efficient caching strategies?
-
A. Utilize a direct file upload to the API endpoint, storing images within the application's file system, and returning direct URLs.
-
B. Employ a separate image hosting service (like AWS S3 or Cloudinary) for storage, and have the API solely manage metadata and return signed URLs for image access. ✓
-
C. Integrate an image processing library directly into the API, allowing on-the-fly resizing and format conversion, managing image storage within the application.
-
D. Use a message queue to handle image uploads asynchronously, processing images independently from the primary API requests and storing them within the application's database.
Explanation: Using a dedicated image hosting service leverages their scalability and optimized infrastructure for image storage and delivery. Signed URLs enhance security and allow for finer-grained control over access. Direct file uploads to the API can become a bottleneck and handling image processing within the main API introduces unnecessary complexity and potential performance issues. Asynchronous processing, while useful in many circumstances, doesn't directly address the core scalability concerns of image storage and retrieval.
Q3. You're designing a RESTful API for a social media platform. To handle rate limiting effectively while minimizing the overhead of per-request checks, which approach is MOST efficient for managing a user's request allowance across multiple endpoints within a short timeframe (e.g., posting a comment and then immediately liking a photo)?
-
A. Employing a distributed cache with token-based rate limiting, where tokens are refreshed periodically based on a sliding window algorithm. ✓
-
B. Implementing a per-endpoint rate limiter using a simple counter with a fixed timeout for each endpoint.
-
C. Utilizing a single global rate limiter, applying restrictions based on the user's IP address.
-
D. Implementing a database-backed rate limiter that tracks each individual request and its timestamp.
Explanation: A distributed cache (like Redis) offers high performance and scalability for rate limiting. A token-based system with a sliding window allows for bursts of requests within a time window, providing a more user-friendly experience than strict per-request limits. Global rate limiting by IP is insufficient for complex scenarios with multiple endpoints, while a database-backed solution is less efficient for high-frequency requests.
Q4. You're designing a RESTful API for a social media platform. To efficiently handle user feeds containing thousands of posts, which strategy best balances performance and maintainability when implementing the `/users/{user_id}/feed` endpoint, considering potential pagination and filtering requirements, while prioritizing minimizing the number of database queries and ensuring data consistency?
-
A. Employ a single database query with extensive WHERE clauses and ORDER BY for pagination and filtering.
-
B. Utilize a dedicated cache layer (e.g., Redis) to store pre-aggregated feed data, updating it asynchronously when posts change. ✓
-
C. Implement a complex stored procedure in the database to handle all pagination, filtering, and data retrieval logic.
-
D. Create separate endpoints for each filtering and pagination criteria, leading to a combinatorial explosion of endpoints.
Explanation: While stored procedures can handle complex logic, they can become difficult to maintain and scale. A single database query with extensive clauses can become inefficient for large datasets. A combinatorial explosion of endpoints is poor API design. A cache layer provides optimal performance by pre-aggregating data and minimizing database load, with asynchronous updates ensuring data consistency over time.
Q5. In an API design, which of the following is an example of a "Hypermedia-Driven" approach?
-
A. The API returns a JSON object with a "next" property that contains a URL for the next page of results. ✓
-
B. The API uses a fixed set of URLs that are hardcoded into the client application.
-
C. The API returns a JSON object with a "links" property that contains a list of available actions and their corresponding URLs.
-
D. The API uses a query parameter to control the pagination of results.
Explanation: In a Hypermedia-Driven approach, the API returns a representation of the available actions and their corresponding URLs, allowing the client to discover and navigate the API on its own. This approach is in line with the principles of REST and HATEOAS.
Q6. In an API design, which of the following is an example of a "HATEOAS" (Hypermedia As The Engine Of Application State) principle?
-
A. The API returns a list of resources with their respective URLs.
-
B. The API uses a query parameter to filter resources.
-
C. The API includes links to related resources in the response. ✓
-
D. The API uses a fixed endpoint for all resource operations.
Explanation: HATEOAS is an API design principle that encourages the use of hypermedia links to navigate between resources. This allows clients to discover and interact with related resources without needing to hardcode endpoint URLs.
Q7. In an API design, which of the following is an example of a “Uniform Interface” as described in Roy Fielding’s dissertation?
-
A. “The API should have a single entry point and all resources should be accessed through that point.”
-
B. “The API should use URI templates to define resource identifiers and use HTTP methods to define operations.” ✓
-
C. “The API should use a single data format for all responses and use HTTP status codes to indicate success or failure.”
-
D. “The API should use a consistent naming convention for all resources and use HTTP methods to define operations.”
Explanation: A Uniform Interface is one of the five architectural constraints described by Roy Fielding in his dissertation. It is characterized by resources identified by global identifiers and manipulated using a fixed set of operations.
Q8. In a RESTful API, which of the following is an example of a Hypermedia-Driven API design principle that promotes self-discovery of API endpoints?
-
A. Using HATEOAS links to provide next actions for the client ✓
-
B. Implementing API keys for authentication and authorization
-
C. Utilizing API documentation for client-side discovery
-
D. Using query parameters for filtering and sorting data
Explanation: HATEOAS (Hypermedia As The Engine Of Application State) is a principle of RESTful API design that allows clients to discover available actions and resources by including links to those actions in the response. This enables the client to navigate the API without relying on pre-defined knowledge of the API's structure.
Q9. Which of the following API design patterns is an example of a “flattened” resource structure, where a single request returns a collection of nested resources?
-
A. Resource Hierarchy
-
B. Collection+Member URI ✓
-
C. Resource Profile
-
D. URI Template Fragmentation
Explanation: In a “flattened” resource structure, a single request returns a collection of nested resources. This is achieved through the use of the Collection+Member URI pattern, where a single URI contains both the collection and member identifiers. For example, “/users/123/tweets” would return a collection of tweets belonging to the user with ID 123. This pattern is useful when the client needs to access a large number of related resources in a single request.
Q10. What is the primary benefit of using the "HATEOAS" principle in API design, as described in Roy Fielding's dissertation?
-
A. Improved scalability through reduced database queries
-
B. Enhanced security through automatic link rotation
-
C. Increased discoverability of API endpoints through hypermedia links ✓
-
D. Simplified API maintenance through standardized URI structures
Explanation: HATEOAS (Hypermedia As The Engine Of Application State) is a principle of RESTful API design that allows clients to discover available actions and resources through hypermedia links embedded in the response. This enables clients to navigate the API without requiring a priori knowledge of the available endpoints.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free