1Z0-900 · Question #38
Given the code fragment: How long does this cookie persist?
The correct answer is D. until browser shutdown. Option D is correct because when a Cookie is created without calling setMaxAge() (or with setMaxAge(-1)), it becomes a session cookie - stored only in browser memory and destroyed when the browser window closes. This is the default cookie behavior in the Java Servlet API. A is…
Question
Given the code fragment:
How long does this cookie persist?
Options
- Auntil server shutdown
- Buntil garbage collection in the servlet instance
- Cthis request
- Duntil browser shutdown
How the community answered
(53 responses)- A4% (2)
- B2% (1)
- D94% (50)
Explanation
Option D is correct because when a Cookie is created without calling setMaxAge() (or with setMaxAge(-1)), it becomes a session cookie - stored only in browser memory and destroyed when the browser window closes. This is the default cookie behavior in the Java Servlet API.
- A is wrong - Server shutdown has no bearing on client-side cookies; cookies live in the browser, not the server process.
- B is wrong - Garbage collection of servlet instances is a server-side JVM event and has no connection to browser-stored cookies.
- C is wrong - A cookie does survive the current request; that would describe a request attribute (
request.setAttribute()), not a cookie.
Memory tip: Think of it as three scopes: request dies with the request, a session cookie dies with the browser, and a persistent cookie (set via setMaxAge(seconds) with a positive value) survives browser restarts. No setMaxAge call = session cookie = browser shutdown.
Topics
Community Discussion
No community discussion yet for this question.