Sample questions
Question 1Easy
Which method of HttpServlet decides whether doGet() or doPost() is called?
- init()
- service()
- destroy()
- getServletInfo()
Answer: B. service()
Explanation
HttpServlet.service() inspects the HTTP method of the request and dispatches to the matching doXxx() method.
Step-by-step solution
- Request reaches the servlet.
service() reads the HTTP method.- It calls
doGet, doPost and so on.
Why the other options are wrong
- A.
init() only does one-time setup. - C.
destroy() only releases resources. - D.
getServletInfo() just returns a description string.
Question 2Easy
How many times does the container call init() on a given servlet instance in normal operation?
- Once, before the first request is served
- Before every request, so setup is repeated for each client call
- After every
service() call - Once per client session, when its HttpSession is first created
Answer: A. Once, before the first request is served
Explanation
The container creates one instance, calls init() once, then calls service() for each request and destroy() once at shutdown.
Step-by-step solution
- Container loads the servlet class.
- It calls
init() one time. - Each request goes to
service(). destroy() runs once at unload.
Why the other options are wrong
- B.
init() is not repeated for each request; service() is. - C.
init() runs before service(), never after it. - D. Servlets are not tied to sessions; one instance serves all clients.
Tip: init, service many times, destroy.
Question 3Easy
What two objects does the container pass to a servlet's service() method?
- ServletRequest and ServletResponse
- HttpSession and Cookie, read from the request object
- Connection and Statement, the JDBC database objects
- Filter and FilterChain, used by the doFilter method
Answer: A. ServletRequest and ServletResponse
Explanation
service() takes a ServletRequest and a ServletResponse. HttpServlet then routes the call to doGet, doPost and so on.
Step-by-step solution
- The container receives an HTTP request.
- It wraps the request and the response objects.
- It calls service(ServletRequest, ServletResponse).
Why the other options are wrong
- B. A session or cookie is read from the request; it is not a service argument.
- C. Those are JDBC types, not servlet method arguments.
- D. Those belong to the doFilter method of a filter.
Tip: The request holds the input; the response holds what you send back.
Question 4Easy
Which servlet method does the container call once, when the servlet is removed from service?
- init()
- service()
- doGet()
- destroy()
Answer: D. destroy()
Explanation
destroy() is the end-of-life method. The container calls it once, when it takes the servlet out of service, so you can release resources.
Step-by-step solution
- init() runs once when the servlet starts.
- service() runs for every request.
- destroy() runs once at the end.
Why the other options are wrong
- A. init() runs once at start-up, not at removal.
- B. service() runs for each request, not at shutdown.
- C. doGet() is called only for HTTP GET requests.
Tip: init at the start, destroy at the end, service in between.
Question 5Medium
A client sends a POST to a servlet that only overrides doGet(). What does the default doPost() in HttpServlet do?
- It calls doGet() automatically, then returns whatever doGet returns
- It sends a 405 Method Not Allowed response
- It sends 200 OK with an empty body, so the client sees success
- It throws a NullPointerException because no body was provided
Answer: B. It sends a 405 Method Not Allowed response
Explanation
HttpServlet's default doPost sends back an error saying POST is not supported. It does not fall back to doGet.
Step-by-step solution
- service() picks doPost for a POST request.
- HttpServlet has a default doPost.
- The default reports 405 Method Not Allowed.
Why the other options are wrong
- A. HttpServlet does not forward a POST to doGet; it reports the method as unsupported.
- C. The default does not report success for a method it does not support.
- D. The default is written to send an error response, not to throw.
Tip: Override every HTTP method that your form or API needs.
Question 6Medium
What does @WebServlet(loadOnStartup = 1) (or load-on-startup in web.xml) ask the container to do?
- Call service() every second, forever, on a timer
- Create a brand new servlet object for every single incoming request
- Load and initialise the servlet at start-up, not on first request
- Remove the servlet from memory when the server is stopped and cleaned up
Answer: C. Load and initialise the servlet at start-up, not on first request
Explanation
A zero or positive load-on-startup value tells the container to load and initialise the servlet during start-up. The first user request is then not slowed by set-up.
Step-by-step solution
- Without it, the container may wait for the first request.
- A positive value asks for loading at start-up.
- The set-up work is done before users arrive.
Why the other options are wrong
- A. load-on-startup only controls when the servlet is loaded, not how often it runs.
- B. The default is one instance per declaration, and load-on-startup does not change that.
- D. Removal is done by destroy(); load-on-startup only controls start-up.
Tip: Use it for servlets with slow set-up, such as opening a database connection.
Question 7Hard
A servlet is deployed once and serves many concurrent requests. Which statement is true about its instances and threads?
- It creates one brand-new instance for each incoming request
- It creates one instance per HTTP session, one for each user
- It creates two instances, one for GET and one for POST
- One instance is shared by all threads, so instance fields are shared
Answer: D. One instance is shared by all threads, so instance fields are shared
Explanation
By default the container creates one servlet instance and calls service() on it from many threads at once. Mutable instance fields are therefore shared and must be thread-safe.
Step-by-step solution
- The container creates one instance per servlet declaration by default.
- Concurrent requests run on separate threads.
- All of those threads share the same instance fields.
Why the other options are wrong
- A. By default the container reuses one instance for many requests.
- B. Sessions hold user data; they do not create servlet instances.
- C. HTTP methods are dispatched inside the same instance.
Tip: Keep per-request data in local variables or the request object, not in instance fields.
Practise all 7 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 →