Bird
Raised Fist0
Rest APIprogramming~20 mins

Response headers (Cache-Control, ETag) in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cache-Control & ETag Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this HTTP response header?

Consider a REST API response with the following headers:

Cache-Control: max-age=3600, must-revalidate
ETag: "abc123"

If a client sends a request with header If-None-Match: "abc123", what will the server most likely respond with?

AHTTP 304 Not Modified with no body
BHTTP 200 OK with full response body
CHTTP 500 Internal Server Error
DHTTP 403 Forbidden
Attempts:
2 left
💡 Hint

ETag helps the server decide if the resource has changed.

🧠 Conceptual
intermediate
1:30remaining
Which Cache-Control directive forces revalidation every time?

Which Cache-Control directive ensures that the client must check with the server before using a cached response?

Apublic
Bmax-age=3600
Cno-cache
Dprivate
Attempts:
2 left
💡 Hint

Think about which directive means 'always check before use'.

❓ Predict Output
advanced
2:00remaining
What is the effect of this Cache-Control header?

Given this response header:

Cache-Control: private, max-age=0, must-revalidate

What does this mean for caching behavior?

AThe response is not cached anywhere
BThe response is cached by any cache and never revalidated
CThe response is publicly cached and served without revalidation for 1 hour
DThe response is cached only by the browser and must be revalidated before each use
Attempts:
2 left
💡 Hint

Consider what private and max-age=0 imply together.

❓ Predict Output
advanced
1:30remaining
What error occurs with this ETag usage?

Consider this server code snippet that sets ETag header incorrectly:

response.headers['ETag'] = 12345

What error or behavior will this cause?

ATypeError because header value must be a string
BSyntaxError due to invalid header format
CThe ETag header is ignored silently
DThe server sends ETag as a string "12345" without error
Attempts:
2 left
💡 Hint

HTTP headers must be strings.

🧠 Conceptual
expert
2:30remaining
How does ETag improve REST API efficiency?

Which statement best explains how ETag headers improve REST API efficiency?

AETag forces servers to always send full responses to clients
BETag enables clients to send conditional requests to avoid downloading unchanged resources
CETag allows clients to cache responses indefinitely without revalidation
DETag disables caching and forces fresh data every time
Attempts:
2 left
💡 Hint

Think about how clients can save bandwidth using ETag.

Practice

(1/5)
1. What is the main purpose of the Cache-Control header in an HTTP response?
easy
A. To indicate the response content type
B. To encrypt the response data for security
C. To specify the server's IP address
D. To tell browsers how long to keep the response data

Solution

  1. Step 1: Understand the role of Cache-Control

    The Cache-Control header instructs browsers and caches how long they can store the response data before requesting it again.
  2. Step 2: Compare options with Cache-Control purpose

    Only To tell browsers how long to keep the response data correctly describes this behavior. Other options describe unrelated functions.
  3. Final Answer:

    To tell browsers how long to keep the response data -> Option D
  4. Quick Check:

    Cache-Control = Duration for caching [OK]
Hint: Cache-Control controls data storage time in browsers [OK]
Common Mistakes:
  • Confusing Cache-Control with security headers
  • Thinking Cache-Control sets content type
  • Assuming Cache-Control reveals server info
2. Which of the following is the correct syntax to set a Cache-Control header to cache content for 3600 seconds?
easy
A. "Cache-Control: max-age=3600"
B. "Cache-Control: timeout=3600"
C. "Cache-Control: expires=3600"
D. "Cache-Control: duration=3600"

Solution

  1. Step 1: Recall Cache-Control max-age syntax

    The correct way to specify caching time in seconds is using max-age=seconds.
  2. Step 2: Check each option's correctness

    Only "Cache-Control: max-age=3600" uses max-age=3600, which is valid. Others use invalid directives.
  3. Final Answer:

    "Cache-Control: max-age=3600" -> Option A
  4. Quick Check:

    max-age sets cache time [OK]
Hint: Use max-age to set cache time in seconds [OK]
Common Mistakes:
  • Using 'timeout' instead of 'max-age'
  • Confusing Cache-Control with Expires header syntax
  • Using unsupported directives like 'duration'
3. Given this HTTP response header snippet:
Cache-Control: max-age=0, must-revalidate
ETag: "abc123"

What happens when the browser sends a request with header If-None-Match: "abc123"?
medium
A. Server returns 304 Not Modified if content unchanged
B. Server returns full content ignoring ETag
C. Server returns 500 Internal Server Error
D. Server always returns 200 OK with new ETag

Solution

  1. Step 1: Understand ETag and If-None-Match interaction

    The ETag header is a unique identifier for the content version. The browser sends If-None-Match with the ETag value to check if content changed.
  2. Step 2: Interpret Cache-Control and server response

    With max-age=0 and must-revalidate, the browser asks server to validate content. If ETag matches, server responds with 304 Not Modified, saving bandwidth.
  3. Final Answer:

    Server returns 304 Not Modified if content unchanged -> Option A
  4. Quick Check:

    ETag + If-None-Match = 304 if unchanged [OK]
Hint: ETag + If-None-Match = 304 Not Modified if same [OK]
Common Mistakes:
  • Assuming server always sends full content
  • Confusing 304 with error codes
  • Ignoring Cache-Control directives
4. A developer sets the response header as Cache-Control: max-age=abc. What is the likely result?
medium
A. The browser caches the response for 'abc' seconds
B. The server returns a syntax error
C. The browser ignores the Cache-Control header and does not cache
D. The browser caches the response indefinitely

Solution

  1. Step 1: Analyze max-age value validity

    The max-age directive requires a numeric value in seconds. 'abc' is invalid.
  2. Step 2: Understand browser behavior on invalid Cache-Control

    Browsers typically ignore invalid Cache-Control headers and treat the response as non-cacheable.
  3. Final Answer:

    The browser ignores the Cache-Control header and does not cache -> Option C
  4. Quick Check:

    Invalid max-age causes no caching [OK]
Hint: max-age must be a number, else ignored [OK]
Common Mistakes:
  • Thinking server returns error for header syntax
  • Assuming browser caches with invalid value
  • Confusing max-age with other headers
5. You want to optimize a REST API to reduce bandwidth by sending full data only when it changes. Which combination of headers should you use in the response?
hard
A. Cache-Control: max-age=0 and no ETag
B. Cache-Control: no-cache and ETag with unique value
C. Cache-Control: no-store and no ETag
D. Cache-Control: max-age=3600 and no ETag

Solution

  1. Step 1: Identify headers that enable conditional requests

    ETag provides a unique identifier for content versions, allowing clients to ask if content changed.
  2. Step 2: Understand Cache-Control no-cache directive

    no-cache forces the browser to validate with the server before using cached data, ensuring fresh content without unnecessary full data transfer.
  3. Step 3: Evaluate other options

    Options without ETag cannot validate changes efficiently; no-store disables caching; max-age=0 without ETag disables validation.
  4. Final Answer:

    Cache-Control: no-cache and ETag with unique value -> Option B
  5. Quick Check:

    no-cache + ETag = bandwidth saving [OK]
Hint: Use no-cache with ETag for efficient validation [OK]
Common Mistakes:
  • Omitting ETag disables change detection
  • Using no-store disables caching entirely
  • Assuming max-age alone reduces bandwidth
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard