Sample questions
Question 1Easy
How can a servlet remove a cookie from the browser?
- Call
request.removeCookie(name) on the incoming request object - Send a cookie with the same name and path and
setMaxAge(0) - Set its value to null on the request and let the browser notice it
- 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
- Create Cookie with same name and path.
- Call
setMaxAge(0). - 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?
- setCookie(cookie)
- sendCookie(cookie)
- writeCookie(cookie)
- 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
- The browser can store a cookie only if the server sends it.
- response.addCookie(cookie) adds a Set-Cookie header.
- 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?
- It stops page JavaScript from reading the cookie through document.cookie
- It makes the cookie work only over HTTPS
- It deletes the cookie when the browser closes
- 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
- HttpOnly is an attribute in the Set-Cookie header.
- Browsers then withhold the cookie from document.cookie.
- 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?
- The session lasts exactly 600 milliseconds
- The session expires 600 minutes after creation regardless of use
- It limits the session to 600 attributes
- 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
- Each request resets the idle timer.
- 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?
- The session expires after 1 second
- The session never times out from inactivity
- The session is invalidated immediately
- 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
- The value is an inactivity timeout in seconds.
- A negative value means no inactivity timeout.
- 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?
- null is returned
- The old cart value is returned
- An IllegalStateException is thrown
- 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
- invalidate() ends the session.
- Calling methods on that session object afterwards is not allowed.
- 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?
- A new session is created and returned
- An empty session with no id
- null
- 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
- Container looks for a session id in the request.
- None found and flag is false.
- 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?
- HttpSession.setId(newId)
- HttpServletResponse.renewSession()
- HttpSession.regenerate()
- 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
- Session fixation means an attacker sets a known session ID before login.
- changeSessionId() issues a new ID for the same session data.
- 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 →