Web Development APIs and REST 3 — Questions and Answers
Question 1: What is the difference between REST and GraphQL regarding data fetching?
- REST always fetches less data than GraphQL
- GraphQL lets clients specify exactly what data they need (Correct answer)
- REST supports real-time subscriptions; GraphQL does not
- GraphQL requires XML; REST requires JSON
Correct answer: GraphQL lets clients specify exactly what data they need
GraphQL allows clients to request precisely the fields they need, eliminating over-fetching and under-fetching problems common in REST.
Question 2: Which header is used to send a bearer token for API authentication?
- X-Auth-Token: Bearer <token>
- Authorization: Bearer <token> (Correct answer)
- Token: Bearer <token>
- API-Key: Bearer <token>
Correct answer: Authorization: Bearer <token>
The Authorization header with the 'Bearer' scheme is the standard way to pass JWT or OAuth tokens in HTTP requests.
Question 3: What is API versioning primarily used for?
- Improving response speed
- Allowing breaking changes without disrupting existing clients (Correct answer)
- Encrypting API responses
- Reducing server memory usage
Correct answer: Allowing breaking changes without disrupting existing clients
API versioning lets you release breaking changes in a new version (e.g., /v2/) while existing clients continue using the old version without disruption.
Question 4: In REST API design, what is 'over-fetching'?
- Sending too many API requests per second
- Receiving more data than the client actually needs (Correct answer)
- Requesting a resource that doesn't exist
- Caching more responses than necessary
Correct answer: Receiving more data than the client actually needs
Over-fetching occurs when an API endpoint returns more fields than the client needs, wasting bandwidth and processing time.
Question 5: What does the 'Content-Negotiation' process do in HTTP?
- Negotiates encryption strength between client and server
- Allows the client to request a preferred response format (Correct answer)
- Handles authentication token exchange
- Validates API request payloads
Correct answer: Allows the client to request a preferred response format
Content negotiation lets clients specify preferred response formats (e.g., JSON or XML) via the Accept header, and the server responds accordingly.
Question 6: Which of these is a correct practice for REST API pagination?
- Always return all records in a single response
- Use query parameters like ?page=2&limit=20 to control result sets (Correct answer)
- Pagination is not supported in REST
- Use HTTP headers only, never query parameters
Correct answer: Use query parameters like ?page=2&limit=20 to control result sets
Query parameters such as page and limit (or offset and limit) are a common and RESTful way to implement pagination for large collections.
Question 7: What is an idempotent HTTP method?
- A method that always returns the same response body
- A method where multiple identical requests produce the same server state as one (Correct answer)
- A method that never modifies server state
- A method that only works with JSON payloads
Correct answer: A method where multiple identical requests produce the same server state as one
Idempotency means that making the same request multiple times results in the same server state as making it once; GET, PUT, and DELETE are idempotent.
What is the difference between REST and GraphQL regarding data fetching?