Rayat Shikshan Sanstha’s
Karmaveer Bhaurao Patil Polytechnic, Satara.
Department of Information Technology
Assignment 6
Servlet
1. What is a servlet in Java?
a) A type of coffee b) A server-side Java program c) A type of Java class d) A type of Java exception
2. Which package provides the classes for servlets?
a) javax.servlet b) java.servlet c) javax.server d) java.server
3. Which HTTP methods can a servlet handle?
a) GET and POST b) GET, POST, PUT, and DELETE
c) GET, POST, and UPDATE d) GET, POST, DELETE, and MODIFY
4. In which method should you override to handle GET requests in a servlet?
a) doGet() b) doPost() c) doRequest() d) handleGet()
5. Which of the following is not a servlet life cycle method?
a) init() b) service() c) destroy() d) run()
6. What is the purpose of the `init()` method in a servlet?
a) To perform cleanup tasks before the servlet is destroyed b) To handle client requests
c) To initialize the servlet before serving client requests d) To redirect requests to other servlets
7. Which method is responsible for destroying a servlet?
a) finalize() b) destroy() c) close() d) cleanup()
8. What is the default scope of a servlet?
a) Request b) Session c) Application d) Page
9. Which object represents a client's request to the servlet container?
a) HttpServletRequest b) HttpServletResponse c) ServletContext d) HttpSession
10. Which method can be used to send a response to the client in a servlet?
a) getRequest() b) sendResponse() c) getResponse() d) HttpServletResponse.getWriter()
11. How can you obtain the value of a request parameter in a servlet?
a) Using the `request.getAttribute()` method b) Using the `request.getParameter()` method
c) Using the `request.getSession()` method d) Using the `request.getAttribute()` method
12. What is the default URL mapping for a servlet in web.xml?
a) /servlet/* b) /servlets/* c) /app/* d) /servlet-name/*
13. What is the purpose of the `web.xml` file in a servlet application?
a) To configure servlet mappings b) To define servlet classes
c) To specify HTML templates d) To store database connection details
14. Which HTTP status code indicates that a resource is not found?
a) 200 OK b) 302 Found c) 404 Not Found d) 500 Internal Server Error
15. Which interface must a servlet implement to handle session events?
a) HttpSessionListener b) HttpSessionAttributeListener
c) ServletContextListener d) ServletRequestListener
16. How can you store session data in a servlet?
a) Using the `HttpServletRequest` object b) Using the `ServletContext` object
c) Using the `HttpSession` object d) Using the `HttpServletResponse` object
17. What is the purpose of the `ServletContext` in a servlet application?
a) To store session data b) To manage the servlet life cycle
c) To share data among servlets d) To store servlet configuration parameters
18. How can you set an attribute in the `ServletContext` in a servlet?
a) Using the `setContextAttribute()` method b) Using the `setAttribute()` method
c) Using the `addAttribute()` method d) Using the `context.set()` method
19. What is the purpose of the `RequestDispatcher` interface in servlets?
a) To send an HTTP request to another server
b) To include the content of another resource in the response
c) To forward the request to another resource within the same servlet context
d) To send a response to the client
20. Which of the following is NOT a valid way to create a servlet in Java?
a) Extending the `HttpServlet` class b) Implementing the `Servlet` interface
c) Using the `new` keyword d) Extending the `GenericServlet` class
21. What is a servlet filter used for?
a) To process requests before they reach a servlet b) To filter out invalid HTTP methods
c) To filter out client IP addresses d) To filter out invalid session data
22. Which of the following is true about servlet filters?
a) Filters can only be used with servlets and not with JSP.
b) Filters can modify the response but not the request.
c) Filters cannot change the order of execution of servlets.
d) Filters can be configured in the web.xml file.
23. What is the purpose of the `doFilter()` method in a servlet filter?
a) To forward the request to another servlet b) To send a response to the client
c) To process the request and response d) To destroy the filter
24. How can you invalidate a session in a servlet?
a) session.invalidate() b) session.invalidateSession() c) session.destroy() d) session.remove()
25. Which of the following is NOT a valid method of communication between servlets?
a) Using the `ServletContext` b) Using the `HttpServletResponse`
c) Using the `RequestDispatcher` d) Using HTTP sessions
26. What is the purpose of the `javax.servlet.annotation.WebServlet` annotation?
a) To define a servlet class b) To configure servlet mappings c) To specify request parameters d) To declare a servlet filter
27. Which HTTP method is used to retrieve data from the server in a safe and idempotent way?
a) GET b) POST c) PUT d) DELETE
28. Which method is called when a session is created in a servlet?
a) sessionCreated() b) sessionInitialized() c) initSession() d) createSession()
29. How can you obtain the session ID in a servlet? a)
session.getSessionID() b) session.getID() c) session.getId() d) session.getSessionId()
30. What is the purpose of the `ServletContext.getAttribute()` method?
a) To retrieve a session attribute b) To retrieve a request attribute
c) To retrieve a servlet context attribute d) To retrieve a response attribute
31. What is the purpose of the `HttpServletRequest.setAttribute()` method?
a) To set a session attribute b) To set a request attribute
c) To set a servlet context attribute d) To set a response attribute
32. Which of the following is NOT a valid HTTP status code?
a) 200 OK b) 300 Redirect c) 400 Bad Request d) 500 Internal Server Error
33. What is the purpose of the `ServletContext.getInitParameter()` method?
a) To retrieve a servlet's initialization parameter b) To retrieve a session attribute
c) To retrieve a request attribute d) To retrieve a servlet context attribute
34. What is the purpose of the `HttpServletResponse.setContentType()` method?
a) To set the HTTP response status code b) To set the content length of the response
c) To set the content type of the response d) To send a redirect response
35. Which of the following is NOT a valid content type for an HTTP response?
a) text/html b) application/json c) image/png d) audio/mp3
36. What is the purpose of the `ServletContext.getContextPath()` method?
a) To retrieve the context path of a servlet b) To retrieve the context path of a session
c) To retrieve the context path of a request d) To retrieve the context path of the servlet container
40. What is the purpose of the `HttpServletResponse.setStatus()` method?
a) To set the HTTP response status code b) To set the content type of the response
c) To set the character encoding of the response d) To set the content length of the response
41. Which of the following is NOT a valid HTTP status code for a successful response?
a) 200 OK b) 201 Created c) 204 No Content d) 404 Not Found
42. Which of the following is NOT a valid HTTP response header?
a) Content-Type b) Location c) Set-Cookie d) Request-Method
43. What is the purpose of the `HttpServletResponse.addCookie()` method?
a) To set an HTTP response header b) To add a cookie to the client's browser
c) To add a session attribute d) To add a request parameter
44. Which of the following is NOT a valid method of managing session tracking in servlets?
a) Using cookies b) Using URL rewriting c) Using session IDs d) Using request attributes
45. Which of the following is NOT a valid content type for an HTTP response?
a) text/html b) application/json c) image/jpeg d) audio/wav
46. What is the purpose of the `HttpServletResponse.reset()` method?
a) To reset the servlet's configuration parameters b) To reset the servlet's initialization parameters
c) To reset the HTTP response to its initial state d) To reset the HTTP request to its initial state
47. What is the purpose of the `HttpServletResponse.sendError()` method?
a) To send a redirect response b) To send an error response with a specified status code
c) To send an error response with a status code of 200 OK d) To send a response with no content
48. What is the purpose of the `HttpServletResponse.setStatus()` method?
a) To set the HTTP response status code b) To set the content type of the response
c) To set the character encoding of the response d) To set the content length of the response
49. What is the purpose of the `ServletContext.getContextPath()` method?
a) To retrieve the context path of a servlet b) To retrieve the context path of a session
c) To retrieve the context path of a request d) To retrieve the context path of the servlet container
50. What does the term "servlet container" refer to?
a) A physical container used to ship servlets b) software component manages execution of servlets
c) A data structure for storing servlet instances d) A security mechanism for servlets
51. Which of the following is NOT a valid HTTP response header in a servlet?
a) Content-Type b) Location c) Session-ID d) Set-Cookie
52. What is the purpose of the `ServletContextListener` interface in servlets?
a) To listen for changes in session attributes b) To listen for changes in servlet context attributes
c) To listen for changes in request parameters d) To listen for changes in response headers
53. How can you include the content of another resource in a servlet response?
a) By using the `RequestDispatcher.include()` method b) By using the `HttpServletResponse.include()` method
c) By using the `RequestDispatcher.forward()` method d) By using the `HttpServletResponse.forward()` method
54. How can you obtain the value of a servlet's initialization parameter in a servlet?
a) Using the `initParameter()` method of the `ServletConfig` object
b) Using the `getParameter()` method of the `HttpServletRequest` object
c) Using the `getAttribute()` method of the `ServletContext` object
d) Using the `getParameter()` method of the `HttpServletResponse` object
55. How is the dynamic interception of requests and responses to transform the information done.
a) sevlet container b)servlet config c)servlet context d)sevlet filter
56. Which type of servletengine is a server that includes built-in support for servlets.
a) Add-onServletEngine b)EmbeddedServletEngine c)StandaloneServletEngine d)None of the above
57. What type of servlet use this method
doGet(),doPost(),doHead(),doDelete(),doTrace(). a)GenericServlet b)HttpServlet c)All
of the above d)None of the above
58. Which Cookie it is valid for single session only and it is removed each time when the user closes the
browser. a)President Cookie
b) Non-president cookie
c)all of the above
d) None of the above
59. Session is the part of the SessionTracking and it is for maintaining the client state at server side.
a) true b)false c)none d)All
60. When destroy() method of a filter is called?
a) the destroy() method is called only once at the end of the life cycle of a filter
b) the destroy() method is called after the filter has executed the doFilter() method
c)the destroy() method is called only once at the beginning of the life cycle of a filter
d) the destroy() method is called after the filter has executed
61. Web server is used for loading the init() method of servlet.
a) true b)false c)None d)All
62. Servlets handle multiple simultaneous requests by using threads.
a) true b)false c)None d)All
63. Which method is used send the same request and response objects to another servlet in RequestDispatcher().
a) forward b)sendRedirect() c)Both a and a d)None of the above