1. What is a servlet?
Answer: A Java program that runs on a server
2. Which interface must be implemented by all servlets?
Answer: Servlet
3. What method is called to initialize a servlet?
Answer: init()
4. Which method is used to handle HTTP GET requests in a servlet?
Answer: doGet()
5. Which object is used to read data from a client request in a servlet?
Answer: HttpServletRequest
6. What is the role of the `ServletConfig` object?
Answer: To provide configuration information to the servlet
7. Which of the following is true about the servlet lifecycle?
Answer: A servlet is loaded into memory only once.
8. What is the purpose of the `doPost()` method in a servlet?
Answer: To process data from an HTML form using the POST method
9. Which of the following classes is used to send binary data in a response
from a servlet?
Answer: ServletOutputStream
10. How can a servlet redirect a request to another resource?
Answer: Using response.sendRedirect()
11. Which annotation is commonly used to declare a servlet in Java EE?
Answer: @WebServlet
12. What is the default HTTP method that a servlet handles if no specific
method is defined?
Answer: GET
13. In a servlet, which method is called to clean up resources before the
servlet is destroyed?
Answer: destroy()
14. What type of object is `HttpServletResponse`?
Answer: It represents the server's response to a client.
15. What is the purpose of the `RequestDispatcher`?
Answer: To forward a request to another resource
16. Which method can be overridden to perform session management in
servlets?
Answer: doGet() (and doPost(), depending on the scenario)
17. What is the main purpose of the `ServletContext` object?
Answer: To manage global application-level parameters and resources
18. What method would you use to retrieve a parameter from a request in
a servlet?
Answer: getParameter()
19. Which of the following can be used to set the content type of the
response in a servlet?
Answer: response.setContentType()
20. Which deployment descriptor file is used to configure servlets in a Java
web application?
Answer: web.xml
21. What is the primary purpose of a servlet?
Answer: To handle server-side requests and generate dynamic responses
22. Which lifecycle method is called when the servlet is first loaded into
memory?
Answer: init()
23. What does the `service()` method do in a servlet?
Answer: Processes requests and generates responses
24. What is the default constructor for a servlet?
Answer: There is no default constructor; it is not needed.
25. Which interface provides methods for reading client data in servlets?
Answer: HttpServletRequest
26. How can servlets maintain state across multiple requests from the
same client?
Answer: All of the above
27. Which of the following is true about the `doPut()` method?
Answer: It is called to handle HTTP PUT requests.
28. In servlet programming, what does "thread-safe" mean?
Answer: Multiple threads can access a resource without issues.
29. Which of the following methods is used to redirect the client to a
different URL?
Answer: response.sendRedirect()
30. What does the `getServletConfig()` method return?
Answer: The configuration parameters for the servlet
31. Which of the following is the correct way to retrieve an attribute from
the request object?
Answer: request.getAttribute()
32. What is the purpose of the `setAttribute()` method in a servlet?
Answer: To store a value in the request or application scope
33. Which of the following is a valid way to create a session in a servlet?
Answer: request.getSession()
34. What does the `response.setStatus(int statusCode)` method do?
Answer: Sets the HTTP status code for the response
35. Which of the following is true about a `Filter` in a servlet context?
Answer: It can modify both request and response objects.
36. How can you include the response of another resource in your
servlet’s response?
Answer: Using request.forward()
37. Which method in the `HttpServlet` class is called to handle HTTP
DELETE requests?
Answer: doDelete()
38. What is the significance of the `web.xml` file in a servlet application?
Answer: It is used to configure servlets and their mappings.
39. What happens if you call `response.sendRedirect()`?
Answer: The client is instructed to make a new request to a different URL.
40. Which of the following scopes can be used to store attributes in a
servlet?
Answer: All of the above
41. What method is used to obtain the current session in a servlet?
Answer: Both B and C
42. Which HTTP method is used to retrieve data from a server?
Answer: GET
43. What is the primary purpose of using `ServletContext`?
Answer: To store application-level parameters
44. Which of the following is used to set a header in the response?
Answer: Both A and B
45. Which method should be used to read form data submitted via POST in
a servlet?
Answer: request.getParameter()
46. What is a servlet filter primarily used for?
Answer: To modify requests and responses
47. How do you retrieve a list of all the parameter names sent in a
request?
Answer: request.getParameterNames()
48. Which of the following describes the relationship between servlets and
JSP?
Answer: JSP pages are compiled into servlets by the server.
49. What method can be used to create cookies in a servlet?
Answer: response.addCookie()
50. Which of the following is a valid way to invalidate a session?
Answer: session.invalidate()
51. What type of data can be sent in the body of a POST request?
Answer: Both text and binary data
52. Which of the following methods is used to read request headers in a
servlet?
Answer: request.getHeader()
53. What does the `getContextPath()` method return?
Answer: The context path of the web application
54. What is the use of `response.setContentType()`?
Answer: To specify the format of the data being sent
55. Which lifecycle method is called to process requests after
initialization?
Answer: service()
56. Which object is used to write data to the response in a servlet?
Answer: Both A and B
57. What does a servlet do when it receives a request?
Answer: It processes the request and generates a response.
58. What is the purpose of `response.flushBuffer()`?
Answer: To send the current response to the client immediately
59. What happens if you call `response.setStatus(404)`?
Answer: The server responds with a "Not Found" status.
60. Which of the following can be used to store application-wide data?
Answer: Application scope
61. What is the purpose of the `doHead()` method in a servlet?
Answer: To return headers only, without the body
62. Which of the following statements about servlet context is true?
Answer: It can be used to share information among all servlets in an
application.
63. What is the purpose of the `setContentLength()` method?
Answer: To set the length of the response body
64. Which method can be overridden to customize the handling of PUT
requests in a servlet?
Answer: doPut()
65. What does the `ServletException` indicate?
Answer: A generic error in the servlet execution
66. Which interface provides methods to manage HTTP sessions?
Answer: HttpSession
67. What is the use of `response.setCharacterEncoding()`?
Answer: To define the character encoding for the response body
68. What does the `getServletName()` method return?
Answer: The name of the servlet as defined in the web.xml
69. Which of the following is true about the `doOptions()` method?
Answer: It is called to handle HTTP OPTIONS requests.
70. How can servlets be made reusable across different applications?
Answer: By defining them in a JAR file
71. What is the role of the `@WebServlet` annotation?
Answer: To specify servlet mapping and configuration
72. Which method would you use to read a cookie in a servlet?
Answer: request.getCookies()
73. What is the behavior of a servlet when it is called for the first time?
Answer: It is loaded and initialized.
74. Which of the following can be used to create a custom error page in a
servlet application?
Answer: Both A and B
75. What happens if a servlet fails to initialize?
Answer: The server throws a ServletException.
76. Which of the following can be used to forward a request to a JSP page
from a servlet?
Answer: Both A and C
77. What is the result of calling `response.setHeader("Cache-Control", "no-
cache")`?
Answer: It disables caching.
78. Which method is used to retrieve all session attributes in a servlet?
Answer: session.getAttributeNames()
79. How do you specify an encoding type for a form submission in HTML?
Answer: By using the `encoding` attribute in the form tag
80. What is the main advantage of using `ServletConfig`?
Answer: It provides configuration information to the servlet
81. What is the main function of the `HttpServlet` class?
**Answer:** To provide a framework for handling HTTP requests and
responses
82. Which method is called when a servlet needs to handle an error?
**Answer:** service()
83. What is the purpose of `response.getWriter()` in a servlet?
**Answer:** To write data to the client in text format
84. Which of the following best describes the `HttpSession` object?
**Answer:** It maintains state across multiple requests from the same
client.
85. What does the `request.getMethod()` method return?
**Answer:** The HTTP method used for the request (GET, POST, etc.)
86. What is the default location for the `web.xml` file in a Java web
application?
**Answer:** /WEB-INF/web.xml
87. What is a `ServletContextListener` used for?
**Answer:** To perform actions during the servlet context lifecycle
88. What is the function of the `setContentType()` method in a servlet?
**Answer:** To define the MIME type of the response
89. Which of the following annotations can be used to map a servlet to a
specific URL?
**Answer:** @WebServlet
90. What is the main advantage of using servlets over CGI?
**Answer:** Servlets can handle multiple requests simultaneously.
91. What is the purpose of the `HttpServletRequest.getSession()` method?
**Answer:** To return the current session associated with the request
92. Which interface allows you to read data sent by the client in a servlet?
**Answer:** HttpServletRequest
93. What type of data can be sent in the body of an HTTP POST request?
**Answer:** Text, binary, and multimedia data
94. What does the `request.getRequestDispatcher("/path")` method
return?
**Answer:** An object that can forward or include a response
95. Which HTTP status code indicates a successful request?
**Answer:** 200
96. What is the function of a `Filter` in a servlet application?
**Answer:** To intercept requests and responses for preprocessing or
postprocessing
97. Which method can be used to retrieve the context path of a servlet?
**Answer:** getContextPath()
98. What does the `response.setHeader("Location", "url")` method
accomplish?
**Answer:** It specifies the URL to which the client should be redirected
99. In servlet programming, which method is called to handle a request
that does not match the doGet() or doPost() methods?
**Answer:** service()
100. What happens if you call `response.sendError(404)` in a servlet?
**Answer:** A custom error page is displayed with the 404 status.
101. What is a cookie in the context of web applications?
**Answer:** A small piece of client-side data stored in the browser
102. How do you create a cookie in a servlet?
**Answer:** Cookie cookie = new Cookie(name, value);
103. Which method is used to add a cookie to the response in a servlet?
**Answer:** response.addCookie(cookie)
104. What is the primary purpose of session tracking in web applications?
**Answer:** To maintain user-specific data across multiple requests
105. How can you invalidate a session in a servlet?
**Answer:** session.invalidate()
106. Which method retrieves a cookie from the request in a servlet?
**Answer:** request.getCookies()
107. What is the maximum size of a cookie that can be stored in a
browser?
**Answer:** 4096 bytes
108. Which of the following statements about session attributes is true?
**Answer:** Session attributes persist for the duration of the session.
109. Which class is used to manage sessions in Java Servlets?
**Answer:** HttpSession
110. How can you set the maximum age of a cookie in seconds?
**Answer:** cookie.setMaxAge(seconds)
111. What method do you use to retrieve the value of a specific cookie in
a servlet?
**Answer:** getCookie(name)
112. Which of the following is true about cookies?
**Answer:** Each cookie can be up to 4 KB in size.
113. What is the default behavior of cookies regarding their path
attribute?
**Answer:** They are accessible from any path on the server.
114. How do you create a new session in a servlet?
**Answer:** request.getSession(true)
115. What happens if you call `request.getSession(false)` when there is
no current session?
**Answer:** It returns null.
116. Which method can be used to set the domain for a cookie?
**Answer:** cookie.setDomain(domain)
117. What is the primary use of the `HttpSession` interface?
**Answer:** To track user sessions and store user-specific data
118. Which of the following is a potential security issue with cookies?
**Answer:** They can be easily manipulated by users.
119. How can you ensure a cookie is only sent over secure connections?
**Answer:** cookie.setSecure(true)
120. What is the typical lifetime of a session created in a servlet?
**Answer:** Until the user explicitly logs out or the session is invalidated
121. What method is used to set a cookie's expiration date?
**Answer:** cookie.setMaxAge(seconds)
122. Which attribute of a cookie defines the URL path for which the cookie
is valid?
**Answer:** Path
123. What is the purpose of the `HttpSession` interface's
`setAttribute(String name, Object value)` method?
**Answer:** To save user-specific data during a session
124. How can you prevent a cookie from being accessed through
JavaScript?
**Answer:** Set the cookie's HttpOnly attribute to true
125. What is the purpose of the `response.setHeader("Set-Cookie",
cookie)` method?
**Answer:** To create or update a cookie in the client's browser
126. Which method would you use to obtain the session ID of a session?
**Answer:** session.getId()
127. What is the result of calling `response.sendRedirect("url")` after
adding a cookie?
**Answer:** The cookie will be sent as part of the redirect response.
128. Which of the following is true about the `HttpSession` object?
**Answer:** It can store objects of any type.
129. What happens to cookies when the browser is closed, assuming they
are session cookies?
**Answer:** They are deleted.
130. What attribute must be set to `true` to restrict a cookie to secure
connections (HTTPS)?
**Answer:** cookie.setSecure(true)
131. What does the `HttpServletRequest.getParameter(String name)`
method return?
**Answer:** The value of a specific parameter sent with the request
132. Which method is used to remove a cookie from the client's browser?
**Answer:** cookie.setMaxAge(0)
133. What type of data is typically sent in a URL query string?
**Answer:** Text data
134. How can you read data sent in a multipart/form-data request?
**Answer:** Using a dedicated library like Apache Commons FileUpload
135. What is the role of the `doPut()` method in a servlet?
**Answer:** To handle PUT requests
136. What HTTP status code is used to indicate that a resource has been
permanently moved?
**Answer:** 301
137. Which interface must be implemented to create a filter in a servlet
application?
**Answer:** Filter
138. What is the purpose of the `init()` method in a servlet?
**Answer:** To initialize the servlet and its resources
139. How can you obtain the client’s IP address in a servlet?
**Answer:** request.getRemoteAddr()
140. What does the `response.setContentType("text/html")` method do?
**Answer:** It defines the MIME type of the response
141. What is the role of the `doDelete()` method in a servlet?
**Answer:** To handle DELETE requests
142. Which annotation is used to define a servlet's initialization
parameters?
**Answer:** @WebInitParam
143. What does the `HttpServletRequest.getHeader(String name)`
method return?
**Answer:** The value of the specified request header
144. Which method is used to retrieve all session attributes in a servlet?
**Answer:** session.getAttributeNames()
145. What is the purpose of the `destroy()` method in a servlet?
**Answer:** To clean up resources before the servlet is destroyed
146. How do you create a URL pattern for a servlet?
**Answer:** Both A and C
147. What does the `response.getOutputStream()` method return?
**Answer:** An output stream for binary data
148. How can you forward a request to another resource in a servlet?
**Answer:** request.getRequestDispatcher("/path").forward(request,
response)
149. What is the default scope of session attributes?
**Answer:** Session
150. Which HTTP method is generally used to submit data to a server?
**Answer:** POST
151. How do you retrieve the context parameter defined in web.xml?
**Answer:** servletContext.getInitParameter(name)
152. What is the purpose of the `setCharacterEncoding()` method in a
servlet?
**Answer:** To set the character encoding for the request
153. What is the main benefit of using a servlet filter?
**Answer:** To perform preprocessing and postprocessing on requests and
responses
154. Which of the following methods is used to send a redirect response to
the client?
**Answer:** response.sendRedirect(url)
155. What is the function of the `getServletConfig()` method in a servlet?
**Answer:** To retrieve the servlet's initialization parameters
156. Which of the following is true about servlet lifecycle methods?
**Answer:** service() is called for each request
157. How can you add a custom error page for a specific error code in
web.xml?
**Answer:**
<error-page><error-code>404</error-code><location>/error.html</locati
on></error-page>
158. What does the `request.getContentType()`
method return?
**Answer:** The MIME type of the request body
159. How do you access the request attributes in a servlet?
**Answer:** request.getAttribute(name)
160. What is the result of calling
`session.setMaxInactiveInterval(seconds)`?
**Answer:** It sets the maximum time interval between client requests
before the session is invalidated.