AJP Questions With Options and Answers
AJP Questions With Options and Answers
9. Which of the following classes is used to send binary data in a response from a
servlet?
A) PrintWriter
B) ServletOutputStream
C) HttpServletResponse
D) BufferedWriter
**Answer:** B) ServletOutputStream
12. What is the default HTTP method that a servlet handles if no specific method is
defined?
A) POST
B) GET
C) PUT
D) DELETE
**Answer:** B) GET
13. In a servlet, which method is called to clean up resources before the servlet
is destroyed?
A) destroy()
B) close()
C) cleanUp()
D) finalize()
**Answer:** A) destroy()
18. What method would you use to retrieve a parameter from a request in a servlet?
A) getParameter()
B) retrieveParameter()
C) fetchParameter()
D) requestParameter()
**Answer:** A) getParameter()
19. Which of the following can be used to set the content type of the response in a
servlet?
A) response.setContentType()
B) response.setHeader()
C) response.setMimeType()
D) response.addHeader()
**Answer:** A) response.setContentType()
20. Which deployment descriptor file is used to configure servlets in a Java web
application?
A) web.xml
B) servlet.xml
C) app.xml
D) config.xml
**Answer:** A) web.xml
22. Which lifecycle method is called when the servlet is first loaded into memory?
A) service()
B) init()
C) destroy()
D) configure()
**Answer:** B) init()
25. Which interface provides methods for reading client data in servlets?
A) HttpServletResponse
B) ServletContext
C) ServletRequest
D) HttpServletRequest
**Answer:** D) HttpServletRequest
26. How can servlets maintain state across multiple requests from the same client?
A) Using URL parameters
B) Using hidden form fields
C) Using sessions
D) All of the above
**Answer:** D) All of the above
29. Which of the following methods is used to redirect the client to a different
URL?
A) response.redirect()
B) response.sendRedirect()
C) request.forward()
D) response.forward()
**Answer:** B) response.sendRedirect()
31. Which of the following is the correct way to retrieve an attribute from the
request object?
A) request.getAttribute()
B) request.retrieveAttribute()
C) request.getParam()
D) request.fetchAttribute()
**Answer:** A) request.getAttribute()
36. How can you include the response of another resource in your servlet’s
response?
A) Using response.redirect()
B) Using request.include()
C) Using request.forward()
D) Using response.include()
**Answer:** C) Using request.forward()
37. Which method in the `HttpServlet` class is called to handle HTTP DELETE
requests?
A) doDelete()
B) delete()
C) handleDelete()
D) processDelete()
**Answer:** A) doDelete()
40. Which of the following scopes can be used to store attributes in a servlet?
A) Request scope
B) Session scope
C) Application scope
D) All of the above
**Answer:** D) All of the above
45. Which method should be used to read form data submitted via POST in a servlet?
A) request.getParameter()
B) request.getPostData()
C) request.readFormData()
D) request.getBody()
**Answer:** A) request.getParameter()
47. How do you retrieve a list of all the parameter names sent in a request?
A) request.getParameterNames()
B) request.getAllParameterNames()
C) request.getParameterList()
D) request.getNames()
**Answer:** A) request.getParameterNames()
48. Which of the following describes the relationship between servlets and JSP?
A) JSP is a newer technology and does not use servlets.
B) JSP pages are compiled into servlets by the server.
C) Servlets are used to manage static content only.
D) There is no relationship between the two.
**Answer:** B) JSP pages are compiled into servlets by the server.
51. What type of data can be sent in the body of a POST request?
A) Only text data
B) Only binary data
C) Both text and binary data
D) No data can be sent
**Answer:** C) Both text and binary data
52. Which of the following methods is used to read request headers in a servlet?
A) request.getHeaders()
B) request.getHeader()
C) request.readHeaders()
D) request.getAllHeaders()
**Answer:** B) request.getHeader()
64. Which method can be overridden to customize the handling of PUT requests in a
servlet?
A) doPost()
B) doPut()
C) doPatch()
D) doOptions()
**Answer:** B) doPut()
73. What is the behavior of a servlet when it is called for the first time?
A) It is loaded and initialized.
B) It sends a 404 error.
C) It is not executed.
D) It creates a new session.
**Answer:** A) It is loaded and initialized.
74. Which of the following can be used to create a custom error page in a servlet
application?
A) web.xml configuration
B) Using the `response.sendError()` method
C) Both A and B
D) Only through annotations
**Answer:** C) Both A and B
76. Which of the following can be used to forward a request to a JSP page from a
servlet?
A) request.forward()
B) response.forward()
C) request.include()
D) Both A and C
**Answer:** D) Both A and C
79. How do you specify an encoding type for a form submission in HTML?
A) By using the `encoding` attribute in the form tag
B) By specifying `Content-Type` in the header
C) By setting `response.setCharacterEncoding()`
D) It cannot be specified in HTML
**Answer:** A) By using the `encoding` attribute in the form tag
**Answer:** C) service()
83. What is the purpose of `response.getWriter()` in a servlet?
A) To write data to the client in text format
B) To log server-side errors
C) To create a new session
D) To read input from the client
**Answer:** B) It maintains state across multiple requests from the same client.
**Answer:** C) 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?
A) /WEB-INF/web.xml
B) /META-INF/web.xml
C) /src/web.xml
D) /config/web.xml
**Answer:** A) /WEB-INF/web.xml
89. Which of the following annotations can be used to map a servlet to a specific
URL?
A) @WebServlet
B) @ServletMapping
C) @URLMapping
D) @RequestMapping
**Answer:** A) @WebServlet
92. Which interface allows you to read data sent by the client in a servlet?
A) ServletResponse
B) HttpServletRequest
C) ServletConfig
D) HttpServlet
**Answer:** B) HttpServletRequest
93. What type of data can be sent in the body of an HTTP POST request?
A) Only text data
B) Only binary data
C) Text, binary, and multimedia data
D) Only JSON data
**Answer:** C) 200
97. Which method can be used to retrieve the context path of a servlet?
A) getServletPath()
B) getContextPath()
C) getPathInfo()
D) getRequestURL()
**Answer:** B) getContextPath()
99. In servlet programming, which method is called to handle a request that does
not match the doGet() or doPost() methods?
A) service()
B) handle()
C) default()
D) doRequest()
**Answer:** A) service()
**Answer:** A) response.addCookie(cookie)
**Answer:** C) session.invalidate()
**Answer:** B) request.getCookies()
107. What is the maximum size of a cookie that can be stored in a browser?
A) 4096 bytes
B) 2048 bytes
C) 8192 bytes
D) 1024 bytes
**Answer:** A) HttpSession
110. How can you set the maximum age of a cookie in seconds?
A) cookie.setMaxAge(seconds)
B) cookie.setAge(seconds)
C) cookie.setExpiration(seconds)
D) cookie.setTimeout(seconds)
**Answer:** A) cookie.setMaxAge(seconds)
111. What method do you use to retrieve the value of a specific cookie in a
servlet?
A) getCookieValue(name)
B) getCookies()[name]
C) getValue(name)
D) getCookie(name)
**Answer:** D) getCookie(name)
112. Which of the following is true about cookies?
A) Cookies can only store text data.
B) Cookies are stored on the server.
C) Each cookie can be up to 4 KB in size.
D) Cookies cannot have an expiration date.
113. What is the default behavior of cookies regarding their path attribute?
A) They are accessible from any path on the server.
B) They are accessible only from the root path.
C) They are inaccessible to any path.
D) They are accessible from the same directory only.
**Answer:** B) request.getSession(true)
116. Which method can be used to set the domain for a cookie?
A) cookie.setDomain(domain)
B) cookie.setCookieDomain(domain)
C) cookie.addDomain(domain)
D) cookie.setCookieAttributes(domain)
**Answer:** A) cookie.setDomain(domain)
119. How can you ensure a cookie is only sent over secure connections?
A) cookie.setSecure(true)
B) cookie.setHttpOnly(true)
C) cookie.setSecureOnly(true)
D) cookie.setEncryption(true)
**Answer:** A) cookie.setSecure(true)
**Answer:** C) Until the user explicitly logs out or the session is invalidated
**Answer:** B) cookie.setMaxAge(seconds)
122. Which attribute of a cookie defines the URL path for which the cookie is
valid?
A) Domain
B) Path
C) Expiration
D) Secure
**Answer:** B) Path
124. How can you prevent a cookie from being accessed through JavaScript?
A) Set the cookie's secure attribute to true
B) Set the cookie's HttpOnly attribute to true
C) Set the cookie's path to /
D) Cookies cannot be prevented from JavaScript access.
126. Which method would you use to obtain the session ID of a session?
A) session.getId()
B) session.getSessionId()
C) request.getSessionId()
D) request.getId()
**Answer:** A) session.getId()
129. What happens to cookies when the browser is closed, assuming they are session
cookies?
A) They are deleted.
B) They persist until their expiration time is reached.
C) They remain until the user clears their history.
D) They are automatically backed up.
**Answer:** A) cookie.setSecure(true)
132. Which method is used to remove a cookie from the client's browser?
A) response.deleteCookie(cookie)
B) cookie.setMaxAge(0)
C) response.removeCookie(cookie)
D) cookie.expire()
**Answer:** B) cookie.setMaxAge(0)
136. What HTTP status code is used to indicate that a resource has been permanently
moved?
A) 404
B) 302
C) 301
D) 500
**Answer:** C) 301
**Answer:** B) Filter
**Answer:** A) request.getRemoteAddr()
**Answer:** A) @WebInitParam
**Answer:** B) session.getAttributeNames()
**Answer:** C) Session
**Answer:** B) POST
**Answer:** D) servletContext.getInitParameter(name)
154. Which of the following methods is used to send a redirect response to the
client?
A) response.sendRedirect(url)
B) response.redirect(url)
C) response.setHeader("Location", url)
D) response.forward(url)
**Answer:** A) response.sendRedirect(url)
155. What is the function of the `getServletConfig()` method in a servlet?
A) To retrieve the servlet's initialization parameters
B) To obtain the servlet's context
C) To handle requests
D) To set response headers
157. How can you add a custom error page for a specific error code in web.xml?
A) <error-page><error-code>404</error-code><location>/error.html</location></error-
page>
B) <error><code>404</code><page>/error.html</page></error>
C) <error-page><code>404</code><uri>/error.html</uri></error-page>
D) <error-handler><code>404</code><url>/error.html</url></error-handler>
**Answer:** A)
<error-page><error-code>404</error-code><location>/error.html</location></error-
page>
159. Which class provides methods to read incoming request data in a servlet?
A) ServletResponse
B) HttpServletRequest
C) ServletConfig
D) HttpServlet
**Answer:** B) HttpServletRequest
**Answer:** B) 15 minutes