Sample questions
Question 1Easy
A form submits tag=a and tag=b in the same request. What does request.getParameterValues("tag").length return?
- 1
- 2
- 0
- It throws an exception
Answer: B. 2
Explanation
getParameterValues returns every value sent under that name, as a String array. Two values were sent, so the length is 2.
Step-by-step solution
- Two tag parameters were sent.
- getParameterValues returns all of them in an array.
- The array has length 2.
Why the other options are wrong
- A. getParameter returns one value; getParameterValues returns all of them.
- C. The parameter exists, so the array is not empty.
- D. It returns an array; a missing name gives null, not an exception.
Tip: getParameterValues for repeated fields.
Question 2Easy
What does request.getParameter("age") return when the request has no parameter called age?
- An empty string
- An exception is thrown because the parameter is missing
- null
- The string "0"
Answer: C. null
Explanation
getParameter returns null if the parameter is absent and "" if it is present with no value. Always null-check before parsing.
Step-by-step solution
- Parameter name is not in the query or form.
- The method returns null.
- Parsing null with
Integer.parseInt would then fail.
Why the other options are wrong
- A. An empty string is returned only when the parameter is present but blank.
- B. No exception is thrown for a missing parameter.
- D. The container never invents a default value like 0.
Tip: Absent gives null; blank gives empty string.
Question 3Easy
Which constant sets the HTTP status to 201 Created in a servlet?
- HttpServletResponse.SC_CREATED
- HttpServletResponse.SC_OK
- HttpServletResponse.SC_ACCEPTED
- HttpServletResponse.SC_NO_CONTENT
Answer: A. HttpServletResponse.SC_CREATED
Explanation
SC_CREATED is the constant for 201. SC_ACCEPTED (202) means the request was accepted but not finished, and SC_NO_CONTENT (204) means success with no body.
Step-by-step solution
- 201 means a resource was created.
- The servlet constant for 201 is SC_CREATED.
- Pass it to response.setStatus().
Why the other options are wrong
- B. 200 means success with a normal body, not a new resource.
- C. 202 means accepted for later processing.
- D. 204 is success with no body.
Tip: 201 is Created; 202 is Accepted; 204 is No Content.
Question 4Medium
Why should response.setContentType("text/html;charset=UTF-8") be called before response.getWriter()?
- getWriter() ignores the content type entirely, so the order of the two calls does not change the output that the browser receives
- The body must be written before the headers can be set
- It stops the browser caching the page
- The writer uses the character encoding in force when it is obtained, so the charset must be set first
Answer: D. The writer uses the character encoding in force when it is obtained, so the charset must be set first
Explanation
Once getWriter() is called, the character encoding is fixed. Setting the charset afterwards does not change the writer, so the content type belongs first.
Step-by-step solution
- getWriter() picks its encoding when it is first called.
- Setting the content type after that does not change the encoding.
- So set the type and charset before calling getWriter().
Why the other options are wrong
- A. The encoding is fixed when the writer is obtained, so the order matters.
- B. Headers must be set before the response is committed, and writing the body can commit it.
- C. Caching is controlled by other headers, not by this call order.
Tip: Set the type first, then get the writer.
Question 5Medium
What is the key difference between response.sendRedirect("b") and RequestDispatcher.forward()?
- Both change the browser URL, but forward is faster because it skips the server
- Redirect works only inside the same web application; forward can go to any site
- Forward sends a 302 status to the client; redirect stays inside the server
- Redirect starts a new browser request and changes the URL; forward does not
Answer: D. Redirect starts a new browser request and changes the URL; forward does not
Explanation
Redirect tells the browser to issue a second request, so the address bar changes and request attributes are lost. Forward is handled inside the server with the same request object.
Step-by-step solution
- Redirect: server replies 302 plus a Location header.
- Browser makes a new request to that URL.
- Forward: server hands the same request to another resource, browser unaware.
Why the other options are wrong
- A. Forward does not change the URL in the browser.
- B. Redirect can point to any URL, including other sites; forward is limited to the same context.
- C. The 302 response belongs to redirect, not forward.
Tip: Redirect = two requests, forward = one.
Question 6Medium
In one servlet, response.getWriter() is called and later response.getOutputStream() is also called on the same response. What happens?
- IllegalStateException
- Both work and share one buffer
- The output stream silently returns null
- The writer is closed and output continues as bytes
Answer: A. IllegalStateException
Explanation
A response may use either the character writer or the binary output stream, not both. Calling the second after the first throws IllegalStateException.
Step-by-step solution
- First call selects text mode.
- Second call asks for binary mode.
- The container rejects it with IllegalStateException.
Why the other options are wrong
- B. The spec forbids using both, so the second call fails, not shares.
- C. getOutputStream does not return null; it throws.
- D. The writer is not auto-closed to switch modes.
Question 7Medium
Servlet A calls request.setAttribute("user", "ann") and then forwards to servlet B with RequestDispatcher.forward. What does B see for request.getAttribute("user")?
- null
- An IllegalStateException
- "ann"
- "ann" only after a redirect
Answer: C. "ann"
Explanation
forward() passes the same request and response objects to the target, so attributes set earlier are still there. A redirect makes a new request, which does not carry them.
Step-by-step solution
- forward() reuses the same request object.
- Attributes live on the request, so B still sees "ann".
- A sendRedirect would start a new request and lose them.
Why the other options are wrong
- A. forward keeps request attributes; only a new request loses them.
- B. getAttribute returns the value and does not throw here.
- D. A redirect is the step that loses attributes, so this is backwards.
Tip: forward keeps the request; redirect starts a new one.
Question 8Hard
Which statement about reading the request body is correct?
- Both can be called, and each one reads the body from the start, so the second read simply gets the same data again
- Calling getInputStream() after getReader() on the same request throws IllegalStateException
- getReader() returns raw bytes with no character decoding
- getInputStream() returns null when the body is empty
Answer: B. Calling getInputStream() after getReader() on the same request throws IllegalStateException
Explanation
The body is read in one mode only. Once getReader() or getInputStream() has been called, calling the other throws IllegalStateException.
Step-by-step solution
- getInputStream() and getReader() are two ways to read the same body.
- Once one is used, the body is consumed in that mode.
- Calling the other one afterwards throws IllegalStateException.
Why the other options are wrong
- A. The body is not rewound, and the second call is not allowed.
- C. getReader decodes characters; getInputStream returns raw bytes.
- D. An empty body gives an empty stream, not null.
Tip: One body, one reading mode.
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 →