Sample questions
Question 1Easy
What does CORS control in a browser?
- Whether a server can read the user disk
- How data is compressed on the network
- Whether a page may read responses from another origin
- How passwords are stored in a database
Answer: C. Whether a page may read responses from another origin
Explanation
CORS is enforced by browsers using headers such as Access-Control-Allow-Origin set by the server.
Step-by-step solution
- Same-origin policy blocks cross-origin reads.
- CORS headers relax it safely.
Why the other options are wrong
- A. Browsers do not give servers disk access.
- B. Compression is a separate header.
- D. CORS is not about storage.
Question 2Easy
Which is a main advantage of token-based (JWT) authentication over server sessions for APIs?
- Tokens can never be stolen
- Tokens remove the need for HTTPS
- The server keeps no per-user session state
- Tokens never expire
Answer: C. The server keeps no per-user session state
Explanation
A signed token carries claims the server can verify, so no session lookup is needed.
Step-by-step solution
- Token is signed.
- Server verifies signature.
- No stored session needed.
Why the other options are wrong
- A. Tokens can be stolen if leaked.
- B. HTTPS is still needed.
- D. Tokens normally carry an expiry.
Question 3Easy
Which statement correctly separates authentication from authorisation?
- They are two names for the same process
- Authorisation happens before authentication
- Authentication is only for admins
- Authentication proves identity; authorisation grants access
Answer: D. Authentication proves identity; authorisation grants access
Explanation
First verify identity, then check permissions.
Step-by-step solution
- Identity first.
- Permissions next.
Why the other options are wrong
- A. They solve different problems.
- B. You must know the user first.
- C. Every user is authenticated.
Question 4Medium
Which technique prevents SQL injection most reliably?
- Parameterised queries (prepared statements)
- Escaping with a custom regex only
- Hiding the database port
- Using longer table names
Answer: A. Parameterised queries (prepared statements)
Explanation
Placeholders keep user input as data, never as part of the SQL command.
Step-by-step solution
- Query text and values are separate.
- Input cannot change the query structure.
Why the other options are wrong
- B. Ad hoc escaping is easy to get wrong.
- C. This does not stop injection through the app.
- D. Names do not affect injection.
Question 5Medium
Where is it generally safer to keep a session or refresh token in a browser app to reduce XSS theft?
- In an HttpOnly, Secure cookie
- In localStorage
- In a global JavaScript variable
- In the URL query string
Answer: A. In an HttpOnly, Secure cookie
Explanation
HttpOnly cookies cannot be read by JavaScript, limiting what an XSS payload can steal.
Step-by-step solution
- XSS runs script in the page.
- HttpOnly hides the cookie from script.
Why the other options are wrong
- B. Any injected script can read localStorage.
- C. Scripts can read it too.
- D. URLs leak through history and logs.
Question 6Medium
In CORS, what is the purpose of a preflight request?
- A request that logs the user in
- A GET request that caches the page
- A request sent by the server to the browser
- An OPTIONS check the browser sends before the real request
Answer: D. An OPTIONS check the browser sends before the real request
Explanation
For non-simple cross-origin requests the browser sends an OPTIONS preflight asking about method and headers.
Step-by-step solution
- Non-simple request detected.
- Browser sends OPTIONS.
- Real request follows if allowed.
Why the other options are wrong
- A. Login is unrelated.
- B. It is an OPTIONS check.
- C. Browsers send it to the server.
Question 7Medium
Two URLs differ: https://app.example.com and https://api.example.com. Are they the same origin?
- No, the host differs, so they are different origins
- Yes, because the scheme and port match
- Yes, because the domain is example.com
- Only if both use cookies
Answer: A. No, the host differs, so they are different origins
Explanation
An origin is scheme + host + port; any difference makes the origins different.
Step-by-step solution
- Scheme: same.
- Host: app vs api, different.
- So not same origin.
Why the other options are wrong
- B. The host is part of the origin.
- C. Subdomains are different origins.
- D. Cookies do not define the origin.
Question 8Medium
What does a salt do when hashing passwords?
- It makes equal passwords hash differently
- It encrypts the password so it can be decrypted later
- It makes the hash shorter
- It lets users reset passwords
Answer: A. It makes equal passwords hash differently
Explanation
A unique random salt per password stops rainbow tables and shows no matches for shared passwords.
Step-by-step solution
- Random salt per user.
- Hash(password + salt).
- Identical passwords differ.
Why the other options are wrong
- B. Hashes are one-way.
- C. Salt does not shorten hashes.
- D. Resets are a separate flow.
Question 9Hard
A site stores a JWT in localStorage. Which attack makes this risky?
- SQL injection on the database
- Cross-site scripting (XSS), since scripts can read it
- Clickjacking in an iframe
- A DDoS flood of requests
Answer: B. Cross-site scripting (XSS), since scripts can read it
Explanation
Any script running on the page can read localStorage, so an XSS bug exposes the token.
Step-by-step solution
- XSS runs attacker script.
- Script reads localStorage.
- Token is exfiltrated.
Why the other options are wrong
- A. SQL injection targets queries, not browser storage.
- C. Clickjacking tricks clicks, it does not read storage.
- D. DDoS affects availability.
Question 10Hard
Which attack does a SameSite cookie attribute primarily help against?
- SQL injection
- Brute-force password guessing
- Man-in-the-middle sniffing
- Cross-site request forgery (CSRF)
Answer: D. Cross-site request forgery (CSRF)
Explanation
SameSite limits when cookies are sent on cross-site requests, which blunts CSRF.
Step-by-step solution
- CSRF relies on cookies sent automatically.
- SameSite limits cross-site sending.
Why the other options are wrong
- A. That needs parameterised queries.
- B. Rate limiting helps there.
- C. HTTPS and Secure cookies help there.
Practise all 11 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 →