Sessions & Cookies Questions with Answers

8 practice questions on Sessions & Cookies (Advanced Java) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

How can a servlet remove a cookie from the browser?

  1. Call request.removeCookie(name) on the incoming request object
  2. Send a cookie with the same name and path and setMaxAge(0)
  3. Set its value to null on the request and let the browser notice it
  4. Call session.invalidate() only

Answer: B. Send a cookie with the same name and path and setMaxAge(0)

Explanation

Cookies live on the browser, so the server sends the same cookie back with max age 0 and the browser deletes it.

Step-by-step solution

  1. Create Cookie with same name and path.
  2. Call setMaxAge(0).
  3. Add it with response.addCookie.

Why the other options are wrong

  • A. HttpServletRequest has no removeCookie method.
  • C. Changing the request does nothing on the client; a response header is needed.
  • D. Invalidating the session removes the session, not other cookies.

Tip: Same name + same path + age 0.

Question 2Easy

Which method on HttpServletResponse sends a cookie to the browser?

  1. setCookie(cookie)
  2. sendCookie(cookie)
  3. writeCookie(cookie)
  4. addCookie(cookie)

Answer: D. addCookie(cookie)

Explanation

addCookie adds a Set-Cookie header to the response. The browser stores the cookie and sends it back on later requests to that site.

Step-by-step solution

  1. The browser can store a cookie only if the server sends it.
  2. response.addCookie(cookie) adds a Set-Cookie header.
  3. The browser returns the cookie on later requests.

Why the other options are wrong

  • A. Not a method on HttpServletResponse.
  • B. The response has no sendCookie method.
  • C. Cookies are sent as headers, not written as body text; use addCookie.

Tip: Cookies go out as headers: add, don't write.

Question 3Easy

What does cookie.setHttpOnly(true) do?

  1. It stops page JavaScript from reading the cookie through document.cookie
  2. It makes the cookie work only over HTTPS
  3. It deletes the cookie when the browser closes
  4. It stops the server from reading the cookie

Answer: A. It stops page JavaScript from reading the cookie through document.cookie

Explanation

The HttpOnly flag hides the cookie from client-side scripts. It does not encrypt the cookie, and the browser still sends it to the server; Secure is the flag for HTTPS only.

Step-by-step solution

  1. HttpOnly is an attribute in the Set-Cookie header.
  2. Browsers then withhold the cookie from document.cookie.
  3. The browser still sends it to the server on each request.

Why the other options are wrong

  • B. That is setSecure(true).
  • C. Expiry is set by maxAge; HttpOnly is unrelated to it.
  • D. The server still receives it on requests.

Tip: HttpOnly guards against scripts, not against the server.

Question 4Medium

Which statement about session.setMaxInactiveInterval(600) is correct?

  1. The session lasts exactly 600 milliseconds
  2. The session expires 600 minutes after creation regardless of use
  3. It limits the session to 600 attributes
  4. The session expires after 600 seconds without a request

Answer: D. The session expires after 600 seconds without a request

Explanation

The value is in seconds and measures time since the last client request. A negative value means the session never times out.

Step-by-step solution

  1. Each request resets the idle timer.
  2. If 600 seconds pass with no request, the container invalidates the session.

Why the other options are wrong

  • A. The unit is seconds, not milliseconds.
  • B. The timer is inactivity-based and restarts on each request.
  • C. It has nothing to do with the attribute count.

Question 5Medium

What does session.setMaxInactiveInterval(-1) do?

  1. The session expires after 1 second
  2. The session never times out from inactivity
  3. The session is invalidated immediately
  4. The session lasts one day after it is created

Answer: B. The session never times out from inactivity

Explanation

A negative value tells the container the session has no inactivity timeout. The session still ends if it is invalidated.

Step-by-step solution

  1. The value is an inactivity timeout in seconds.
  2. A negative value means no inactivity timeout.
  3. The session therefore does not expire from idle time.

Why the other options are wrong

  • A. A positive value is in seconds, and -1 means no timeout.
  • C. That is what invalidate() does; this sets a timeout.
  • D. The limit applies to inactivity, not creation time, and -1 means no limit.

Tip: Negative means no inactivity timeout.

Question 6Medium

A servlet calls session.invalidate() and then session.getAttribute("cart") in the same request. What happens?

  1. null is returned
  2. The old cart value is returned
  3. An IllegalStateException is thrown
  4. A new empty session is created and returned

Answer: C. An IllegalStateException is thrown

Explanation

Once a session is invalidated, its methods, including getAttribute, throw IllegalStateException. Read what you need before calling invalidate().

Step-by-step solution

  1. invalidate() ends the session.
  2. Calling methods on that session object afterwards is not allowed.
  3. getAttribute throws IllegalStateException.

Why the other options are wrong

  • A. A closed session throws rather than returning null.
  • B. The attributes are gone once the session is invalidated.
  • D. invalidate() does not create a session; a later getSession() call would.

Tip: Read session data before invalidating.

Question 7Medium

What does request.getSession(false) return when the client has no existing session?

  1. A new session is created and returned
  2. An empty session with no id
  3. null
  4. An IllegalStateException is thrown

Answer: C. null

Explanation

With false the container returns the current session if one exists, otherwise null. This avoids creating sessions needlessly, for example in a logout servlet.

Step-by-step solution

  1. Container looks for a session id in the request.
  2. None found and flag is false.
  3. It returns null instead of creating one.

Why the other options are wrong

  • A. Only getSession() or getSession(true) creates one.
  • B. No session object is created for the false flag.
  • D. It does not throw; it simply reports no session with null.

Tip: Use getSession(false) in logout.

Question 8Hard

Which method changes the session ID while keeping the session's attributes, as a defence against session fixation after login?

  1. HttpSession.setId(newId)
  2. HttpServletResponse.renewSession()
  3. HttpSession.regenerate()
  4. HttpServletRequest.changeSessionId()

Answer: D. HttpServletRequest.changeSessionId()

Explanation

changeSessionId() (Servlet 3.1 and later) gives the session a new ID and keeps its attributes. Calling it at login means a session ID planted by an attacker before login no longer names the logged-in session.

Step-by-step solution

  1. Session fixation means an attacker sets a known session ID before login.
  2. changeSessionId() issues a new ID for the same session data.
  3. The attacker's known ID no longer names the logged-in session.

Why the other options are wrong

  • A. HttpSession has no setId method.
  • B. No such method exists in the servlet API.
  • C. No such method; the call is changeSessionId() on the request.

Tip: changeSessionId on the request, at login.

Practise all 8 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 Advanced Java topics · All topics