CORS Questions with Answers

13 practice questions on CORS (Modern Web (Next.js, PWA, Performance)) with answers and step-by-step solutions. Try 10 samples below, then practise all 13 free in the AptiNest app.

Sample questions

Question 1Easy

What does CORS let a server do?

  1. Let chosen other origins read its responses
  2. Block all requests that come from mobile apps
  3. Encrypt responses sent to other origins
  4. Bypass the same-origin policy for its own pages

Answer: A. Let chosen other origins read its responses

Explanation

Cross-Origin Resource Sharing relaxes the same-origin policy for responses, using headers such as Access-Control-Allow-Origin.

Step-by-step solution

  1. The same-origin policy blocks other origins from reading responses.
  2. CORS is a server opt-in that relaxes this for chosen origins.
  3. The server sends headers that name the allowed origins.
  4. So the correct answer is letting chosen other origins read its responses.

Why the other options are wrong

  • B. CORS is a browser rule, not a block on mobile apps.
  • C. CORS does not encrypt responses.
  • D. CORS does not bypass the policy for the server's own pages; it opens it for other origins.

Tip: CORS is how a server allows specific other origins to read its responses.

Question 2Easy

What is an origin made of?

  1. Host and path only
  2. Domain and IP address
  3. Scheme and path
  4. Scheme, host and port

Answer: D. Scheme, host and port

Explanation

Two URLs share an origin only if scheme, host and port all match.

Step-by-step solution

  1. An origin identifies a page by who it came from.
  2. It is built from the scheme, the host and the port.
  3. The path is not part of the origin.
  4. So the correct answer is scheme, host and port.

Why the other options are wrong

  • A. Path is not part of the origin, and scheme is missing.
  • B. IP address is not part of the origin definition.
  • C. The path is left out, and the host is missing.

Tip: Origin = scheme + host + port.

Question 3Easy

Which response header names the origins allowed to read a response?

  1. Allow
  2. Access-Control-Allow-Origin
  3. Content-Security-Policy-Origin
  4. Origin

Answer: B. Access-Control-Allow-Origin

Explanation

The browser compares this header with the requesting page's origin before exposing the response to JS.

Step-by-step solution

  1. The browser compares a response header with the page's origin.
  2. The header that names the allowed origin is Access-Control-Allow-Origin.
  3. If it matches, the browser lets JavaScript read the response.
  4. So the correct answer is Access-Control-Allow-Origin.

Why the other options are wrong

  • A. Allow is not a CORS response header.
  • C. Content-Security-Policy-Origin is not a real CORS header.
  • D. Origin is a request header, not the response header that allows reading.

Tip: Access-Control-Allow-Origin is the key CORS response header.

Question 4Easy

Are https://app.example.com and https://api.example.com the same origin?

  1. Only if they share a server
  2. Yes, both use HTTPS
  3. Yes, they share a domain
  4. No, the hosts differ

Answer: D. No, the hosts differ

Explanation

Different subdomains are different hosts, hence different origins.

Step-by-step solution

  1. Origin is scheme, host and port.
  2. app.example.com and api.example.com are different hosts.
  3. Different hosts make different origins.
  4. So the correct answer is no, the hosts differ.

Why the other options are wrong

  • A. Sharing a server does not make the same origin.
  • B. Both using HTTPS is not enough; the hosts still differ.
  • C. Sharing a domain is not the same as sharing an origin.

Tip: Subdomains are different origins; CORS is needed between them.

Question 5Medium

A fetch to another origin gets a CORS error in the browser, but the same request works in curl. Why?

  1. curl uses a different HTTP version that skips CORS
  2. The browser blocks every request to other origins
  3. curl ignores CORS; the headers are missing
  4. The server is down only for browsers

Answer: C. curl ignores CORS; the headers are missing

Explanation

The server responded, but without proper Access-Control-* headers the browser refuses to expose the response to the page.

Step-by-step solution

  1. Curl does not enforce the browser's CORS rules.
  2. The server still answers, so the request works in curl.
  3. The browser blocks the page from reading the response without Access-Control-* headers.
  4. So the correct answer is that curl ignores CORS and the headers are missing.

Why the other options are wrong

  • A. HTTP version does not decide CORS.
  • B. The browser does not block every cross-origin request, only unreadable responses.
  • D. The server is not down for browsers; it responds without CORS headers.

Tip: A CORS error with curl working means the server is missing Access-Control headers.

Question 6Medium

Which request triggers a preflight?

  1. A form POST as text/plain
  2. A PUT with Content-Type: application/json
  3. A plain GET request with no custom headers
  4. A GET for an image tag

Answer: B. A PUT with Content-Type: application/json

Explanation

Methods other than GET/HEAD/POST, or JSON content types, or custom headers make a request non-simple.

Step-by-step solution

  1. Simple requests are GET, HEAD or POST with basic headers and simple content types.
  2. A PUT is not a simple method.
  3. Application/json is not a simple content type, so the request is non-simple.
  4. So the correct answer is a PUT with Content-Type application/json.

Why the other options are wrong

  • A. A form POST as text/plain is a simple request, so no preflight.
  • C. A plain GET with no custom headers is simple.
  • D. An image GET is simple.

Tip: PUT, DELETE, custom headers and application/json trigger a preflight.

Question 7Medium

What is a CORS preflight request?

  1. An OPTIONS request asking if the real one is allowed
  2. A HEAD request used to measure network latency
  3. A request the server sends back to the browser
  4. A GET request sent to warm up the server cache

Answer: A. An OPTIONS request asking if the real one is allowed

Explanation

For non-simple requests the browser first sends OPTIONS with Access-Control-Request-Method/Headers and waits for permission.

Step-by-step solution

  1. A preflight is sent before a non-simple cross-origin request.
  2. It uses the OPTIONS method.
  3. It asks the server whether the real request method and headers are allowed.
  4. So the correct answer is an OPTIONS request asking if the real one is allowed.

Why the other options are wrong

  • B. HEAD is not used for the preflight.
  • C. The server does not send requests to the browser for preflight.
  • D. A GET to warm the cache is not a preflight.

Tip: Preflight = OPTIONS sent before the real request.

Question 8Medium

What does Access-Control-Max-Age do?

  1. Caches the preflight answer for N seconds
  2. Sets the timeout for the real request
  3. Sets the cookie lifetime for cross-site requests
  4. Limits the response body size in bytes

Answer: A. Caches the preflight answer for N seconds

Explanation

Caching the preflight avoids sending an OPTIONS request before every call.

Step-by-step solution

  1. Preflight responses can be cached by the browser.
  2. Access-Control-Max-Age gives the cache time in seconds.
  3. During that time the browser skips the OPTIONS request.
  4. So the correct answer is caching the preflight answer for N seconds.

Why the other options are wrong

  • B. It does not set a timeout for the real request.
  • C. Cookie lifetime is not set by this header.
  • D. It does not limit the response body size.

Tip: Access-Control-Max-Age cuts repeated OPTIONS requests.

Question 9Hard

Which header lets JavaScript read a custom response header such as X-Total-Count across origins?

  1. Access-Control-Request-Headers
  2. Access-Control-Allow-Headers
  3. Vary
  4. Access-Control-Expose-Headers

Answer: D. Access-Control-Expose-Headers

Explanation

By default only CORS-safelisted response headers are readable; Expose-Headers adds more. Allow-Headers concerns request headers.

Step-by-step solution

  1. By default only a few safelisted response headers are readable by JavaScript.
  2. A custom header such as X-Total-Count is hidden unless exposed.
  3. Access-Control-Expose-Headers lists the extra headers to expose.
  4. So the correct answer is Access-Control-Expose-Headers.

Why the other options are wrong

  • A. Request-Headers names the request headers, not what is readable.
  • B. Allow-Headers concerns request headers the client may send.
  • C. Vary changes caching, not header readability.

Tip: Use Access-Control-Expose-Headers to read custom response headers.

Question 10Hard

A server replies to every origin by echoing the request's Origin header with Allow-Credentials: true. What is the risk?

  1. Browsers will cache responses forever
  2. There is no risk; this is the recommended setup
  3. Any website can read users' authenticated data
  4. Only the server's own pages will be blocked

Answer: C. Any website can read users' authenticated data

Explanation

Reflecting any origin with credentials is effectively a wildcard that lets malicious sites make authenticated reads. Use an allow-list.

Step-by-step solution

  1. The server echoes any Origin and allows credentials.
  2. So every website is allowed to read responses with the user's cookies.
  3. A malicious site can then read authenticated data.
  4. So the correct answer is that any website can read users' authenticated data.

Why the other options are wrong

  • A. Browser caching is not the risk here.
  • B. Echoing any origin with credentials is not recommended.
  • D. Other sites are not blocked; they are allowed.

Tip: Never reflect any origin with credentials; use an allow-list.

Practise all 13 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Modern Web (Next.js, PWA, Performance) topics · All topics