x

Java Servlets Questions

PREV     NEXT

Java Servlets Questions:


1. The method getWriter returns an object of type PrintWriter. This class has println methods to generate output. Which of these classes define the getWriter method? Select the one correct answer.

  1. HttpServletRequest
  2. HttpServletResponse
  3. ServletConfig
  4. ServletContext

2. Name the method defined in the HttpServletResponse class that may be used to set the content type. Select the one correct answer.

  1. setType
  2. setContent
  3. setContentType
  4. setResponseContentType

3. Which of the following statement is correct. Select the one correct answer.

  1. The response from the dedicated server to a HEAD request consists of status line, content type and the document.
  2. The response from the server to a GET request does not contain a document.
  3. The setStatus method defined in the HttpServletRequest class takes an int as an argument and sets the status of Http response
  4. The HttpServletResponse defines constants like SC_NOT_FOUND that may be used as a parameter to setStatus method.

4. The sendError method defined in the HttpServlet class is equivalent to invoking the setStatus method with the following parameter. Select the one correct answer.

  1. SC_OK
  2. SC_MOVED_TEMPORARILY
  3. SC_NOT_FOUND
  4. SC_INTERNAL_SERVER_ERROR
  5. ESC_BAD_REQUEST

5. The sendRedirect method defined in the HttpServlet class is equivalent to invoking the setStatus method with the following parameter and a Location header in the URL. Select the one correct answer.

  1. SC_OK
  2. SC_MOVED_TEMPORARILY
  3. SC_NOT_FOUND
  4. SC_INTERNAL_SERVER_ERROR
  5. ESC_BAD_REQUEST

6. Which of the following statements are correct about the status of the Http response. Select the one correct answer.

  1. A status of 200 to 299 signifies that the request was successful.
  2. A status of 300 to 399 are informational messages.
  3. A status of 400 to 499 indicates an error in the server.
  4. A status of 500 to 599 indicates an error in the client.

7. To send binary output in a response, the following method of HttpServletResponse may be used to get the appropriate Writer/Stream object. Select the one correct answer.

  1. getStream
  2. getOutputStream
  3. getBinaryStream
  4. getWriter

8. To send text output in a response, the following method of HttpServletResponse may be used to get the appropriate Writer/Stream object. Select the one correct answer.

  1. getStream
  2. getOutputStream
  3. getBinaryStream
  4. getWriter

9. Is the following statement true or false. URL rewriting may be used when a browser is disabled. In URL encoding the session id is included as part of the URL.

10. Name the class that includes the getSession method that is used to get the HttpSession object.

  1. HttpServletRequest
  2. HttpServletResponse
  3. SessionContext
  4. SessionConfig

11. Which of the following are correct statements? Select the two correct answers.

  1. The getRequestDispatcher method of ServletContext class takes the full path of the servlet, whereas the getRequestDispatcher method of HttpServletRequest class takes the path of the servlet relative to the ServletContext.
  2. The include method defined in the RequestDispatcher class can be used to access one servlet from another. But it can be invoked only if no output has been sent to the server.
  3. The getRequestDispatcher(String URL) is defined in both ServletContext and HttpServletRequest method
  4. The getNamedDispatcher(String) defined in HttpServletRequest class takes the name of the servlet and returns an object of RequestDispatcher class.

12. A user types the URL http://www.javaprepare.com/scwd/index.html . Which HTTP request gets generated. Select the one correct answer.


  1. GET method
  2. POST method
  3. HEAD method
  4. PUT method

13. Which HTTP method gets invoked when a user clicks on a link? Select the one correct answer.

  1. GET method
  2. POST method
  3. HEAD method
  4. PUT method

14. When using HTML forms which of the folowing is true for POST method? Select the one correct answer.

  1. POST allows users to bookmark URLs with parameters.
  2. The POST method should not be used when large amount of data needs to be transferred.
  3. POST allows secure data transmission over the http method.
  4. POST method sends data in the body of the request.

15. Which of the following is not a valid HTTP/1.1 method. Select the one correct answer.

  1. CONNECT method
  2. COMPARE method
  3. OPTIONS method
  4. TRACE method

16. Name the http method used to send resources to the server. Select the one correct answer.

  1. FTP methodd
  2. PUT method
  3. WRITE method
  4. COPY method

17. Name the http method that sends the same response as the request. Select the one correct answer.

  1. DEBUG method
  2. TRACE method
  3. OPTIONS method
  4. HEAD method

18. Which three digit error codes represent an error in request from client? Select the one correct answer.

  1. Codes starting from 200
  2. Codes starting from 300
  3. Codes starting from 400
  4. Codes starting from 500

19. Name the location of compiled class files within a war file? Select the one correct answer.

  1. /META-INF/classes
  2. /classes
  3. /WEB-INF/classes
  4. /root/classes

Answers to questions on Servlets

  1. B. The class HttpServletResponse defines the getWriter method.
  2. B. setContentType sets the content type of the response being sent to the client.
  3. D. The response from the server to a HEAD request does not contain the document, whereas the response to GET request does contain a document. So A and B are incorrect. C is incorrect because setStauts is defined in HttpServletResponse.
  4. C. sendError(String URL) is equivalent to sending SC_NOT_FOUND (404) response code.
  5. B. sendRedirect(String URL) is equivalent to sending SC_MOVED_TEMPORARILY (302) response code and a location header in the URL.
  6. A. The following table specifies the specific the status code of Http response.
    Status Code Purpose
    100-199 Informational
    200-299 Request was successful
    300-399 Request file has moved.
    400-499 Client error
    500-599 Server error.
  7. B. The getOutputStream method is used to get an output stream to send binary data. The getWriter method is used to get a PrintWriter object that can be used to send text data.
  8. D
  9. true. The statement about URL encoding is correct.
  10. A. The class HttpServletRequest defines the getSession method.
  11. A, C.
  12. A. GET method gets invoked when a URL is typed.
  13. A. GET method gets invoked when user clicks on a link.
  14. D. Since POST does not have attributes in the URL, it cannot be used to bookmark the URL. Since arguments are present in the body of the request, using POST method does not guarantee security.
  15. B. COMPARE is not a valid HTTP method.
  16. B. PUT method is used to send resources from client to server.
  17. B. TRACE method is used for debugging. It sends the same response as request.
  18. C. A status code of 4XX represents a client error.
  19. C. Classes are stored in /WEB-INF/classes.

PREV     NEXT



Like it? Please Spread the word!

Post Your Thoughts