0% found this document useful (0 votes)
29 views21 pages

AJP Questions With Options and Answers

Uploaded by

Reyan Dabre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views21 pages

AJP Questions With Options and Answers

Uploaded by

Reyan Dabre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 21

1. What is a servlet?

A) A Java program that runs on a client


B) A Java program that runs on a server
C) A database management tool
D) A web browser extension
**Answer:** B) A Java program that runs on a server

2. Which interface must be implemented by all servlets?


A) ServletConfig
B) ServletRequest
C) ServletResponse
D) Servlet
**Answer:** D) Servlet

3. What method is called to initialize a servlet?


A) init()
B) start()
C) load()
D) begin()
**Answer:** A) init()

4. Which method is used to handle HTTP GET requests in a servlet?


A) doPost()
B) doGet()
C) processRequest()
D) handleRequest()
**Answer:** B) doGet()

5. Which object is used to read data from a client request in a servlet?


A) ServletOutputStream
B) HttpServletRequest
C) ServletResponse
D) RequestDispatcher
**Answer:** B) HttpServletRequest

6. What is the role of the `ServletConfig` object?


A) To manage the servlet lifecycle
B) To provide configuration information to the servlet
C) To handle HTTP requests
D) To manage sessions
**Answer:** B) To provide configuration information to the servlet

7. Which of the following is true about the servlet lifecycle?


A) A servlet is loaded into memory only once.
B) The servlet is destroyed after each request.
C) A new instance of the servlet is created for each request.
D) init() is called after destroy().
**Answer:** A) A servlet is loaded into memory only once.

8. What is the purpose of the `doPost()` method in a servlet?


A) To process data from an HTML form using the POST method
B) To redirect a request to another resource
C) To return HTML content
D) To initialize servlet parameters
**Answer:** A) 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?
A) PrintWriter
B) ServletOutputStream
C) HttpServletResponse
D) BufferedWriter
**Answer:** B) ServletOutputStream

10. How can a servlet redirect a request to another resource?


A) Using `HttpServletRequest`
B) Using `response.sendRedirect()`
C) Using `request.forward()`
D) Using `ServletConfig`
**Answer:** B) Using `response.sendRedirect()`

11. Which annotation is commonly used to declare a servlet in Java EE?


A) @Servlet
B) @WebServlet
C) @HttpServlet
D) @ServletConfig
**Answer:** B) @WebServlet

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()

14. What type of object is `HttpServletResponse`?


A) It represents the client's request.
B) It represents the server's response to a client.
C) It handles session management.
D) It is used for servlet configuration.
**Answer:** B) It represents the server's response to a client.

15. What is the purpose of the `RequestDispatcher`?


A) To send a response to the client
B) To forward a request to another resource
C) To create sessions
D) To initialize servlet parameters
**Answer:** B) To forward a request to another resource

16. Which method can be overridden to perform session management in servlets?


A) doGet()
B) doPost()
C) doPut()
D) doSession()
**Answer:** A) doGet() (and doPost(), depending on the scenario)

17. What is the main purpose of the `ServletContext` object?


A) To provide configuration information for the servlet
B) To manage individual requests and responses
C) To manage global application-level parameters and resources
D) To handle client sessions
**Answer:** C) To manage global application-level parameters and resources

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

21. What is the primary purpose of a servlet?


A) To process client-side scripts
B) To handle server-side requests and generate dynamic responses
C) To manage database connections
D) To serve static HTML pages
**Answer:** B) To handle server-side requests and generate dynamic responses

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()

23. What does the `service()` method do in a servlet?


A) Initializes the servlet
B) Cleans up resources
C) Processes requests and generates responses
D) Configures servlet parameters
**Answer:** C) Processes requests and generates responses

24. What is the default constructor for a servlet?


A) Servlet()
B) HttpServlet()
C) GenericServlet()
D) There is no default constructor; it is not needed.
**Answer:** D) There is no default constructor; it is not needed.

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

27. Which of the following is true about the `doPut()` method?


A) It is used to retrieve data from a client.
B) It is not supported by servlets.
C) It is called to handle HTTP PUT requests.
D) It is the default method for handling requests.
**Answer:** C) It is called to handle HTTP PUT requests.

28. In servlet programming, what does "thread-safe" mean?


A) Multiple threads can access a resource without issues.
B) Only one thread can access a resource at a time.
C) The resource is protected from being accessed by any thread.
D) All threads complete their tasks successfully.
**Answer:** A) Multiple threads can access a resource without issues.

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()

30. What does the `getServletConfig()` method return?


A) The configuration parameters for the servlet
B) The current session data
C) The request attributes
D) The servlet's initialization parameters
**Answer:** A) The configuration parameters for the servlet

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()

32. What is the purpose of the `setAttribute()` method in a servlet?


A) To store a value in the session
B) To send a response back to the client
C) To store a value in the request or application scope
D) To initialize servlet parameters
**Answer:** C) 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?


A) request.createSession()
B) session.create()
C) request.getSession()
D) session.get()
**Answer:** C) request.getSession()

34. What does the `response.setStatus(int statusCode)` method do?


A) Sets the content type of the response
B) Sets the HTTP status code for the response
C) Sets the character encoding for the response
D) Sets a custom header in the response
**Answer:** B) Sets the HTTP status code for the response

35. Which of the following is true about a `Filter` in a servlet context?


A) It can modify both request and response objects.
B) It can only read requests but cannot modify them.
C) It is used to manage database connections only.
D) Filters are the same as servlets.
**Answer:** A) It can modify both request and response objects.

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()

38. What is the significance of the `web.xml` file in a servlet application?


A) It defines database connections.
B) It is used to configure servlets and their mappings.
C) It manages user sessions.
D) It stores servlet source code.
**Answer:** B) It is used to configure servlets and their mappings.

39. What happens if you call `response.sendRedirect()`?


A) The client is instructed to make a new request to a different URL.
B) The response is sent immediately without further processing.
C) The server forwards the request to another servlet.
D) The session is terminated.
**Answer:** A) 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?
A) Request scope
B) Session scope
C) Application scope
D) All of the above
**Answer:** D) All of the above

41. What method is used to obtain the current session in a servlet?


A) request.getCurrentSession()
B) request.getSession()
C) request.getSession(true)
D) Both B and C
**Answer:** D) Both B and C
42. Which HTTP method is used to retrieve data from a server?
A) POST
B) GET
C) PUT
D) DELETE
**Answer:** B) GET

43. What is the primary purpose of using `ServletContext`?


A) To handle session data
B) To store application-level parameters
C) To read client requests
D) To manage servlet configurations
**Answer:** B) To store application-level parameters

44. Which of the following is used to set a header in the response?


A) response.setHeader()
B) response.addHeader()
C) Both A and B
D) response.setResponseHeader()
**Answer:** C) Both A and B

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()

46. What is a servlet filter primarily used for?


A) To modify requests and responses
B) To manage database connections
C) To handle session timeouts
D) To configure servlets
**Answer:** A) To modify requests and responses

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.

49. What method can be used to create cookies in a servlet?


A) response.createCookie()
B) response.addCookie()
C) request.setCookie()
D) response.setCookie()
**Answer:** B) response.addCookie()

50. Which of the following is a valid way to invalidate a session?


A) session.remove()
B) session.invalidate()
C) session.destroy()
D) session.close()
**Answer:** B) session.invalidate()

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()

53. What does the `getContextPath()` method return?


A) The URL of the servlet
B) The path to the servlet’s directory
C) The context path of the web application
D) The request parameters
**Answer:** C) The context path of the web application

54. What is the use of `response.setContentType()`?


A) To define the HTTP method
B) To specify the format of the data being sent
C) To change the response header
D) To configure the servlet
**Answer:** B) To specify the format of the data being sent

55. Which lifecycle method is called to process requests after initialization?


A) init()
B) service()
C) destroy()
D) handleRequest()
**Answer:** B) service()

56. Which object is used to write data to the response in a servlet?


A) ServletOutputStream
B) PrintWriter
C) Both A and B
D) HttpServletResponse
**Answer:** C) Both A and B

57. What does a servlet do when it receives a request?


A) It processes the request and generates a response.
B) It ignores the request.
C) It forwards the request to another server.
D) It terminates the session.
**Answer:** A) It processes the request and generates a response.

58. What is the purpose of `response.flushBuffer()`?


A) To send the current response to the client immediately
B) To clear the response buffer
C) To check if the buffer is empty
D) To reset the response
**Answer:** A) To send the current response to the client immediately

59. What happens if you call `response.setStatus(404)`?


A) The request is processed normally.
B) The server responds with a "Not Found" status.
C) The response is canceled.
D) The client is redirected.
**Answer:** B) The server responds with a "Not Found" status.

60. Which of the following can be used to store application-wide data?


A) Session scope
B) Request scope
C) Application scope
D) All of the above
**Answer:** C) Application scope

61. What is the purpose of the `doHead()` method in a servlet?


A) To return headers only, without the body
B) To process a HEAD request and return a response
C) To handle form submissions
D) To send a file to the client
**Answer:** A) To return headers only, without the body

62. Which of the following statements about servlet context is true?


A) It is unique to each servlet.
B) It can be used to share information among all servlets in an application.
C) It is created for every request.
D) It is only accessible by the main servlet.
**Answer:** B) It can be used to share information among all servlets in an
application.

63. What is the purpose of the `setContentLength()` method?


A) To specify the length of the request body
B) To set the length of the response body
C) To define the maximum size of a request
D) To limit the size of a session
**Answer:** B) To set the length of the response body

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()

65. What does the `ServletException` indicate?


A) A generic error in the servlet execution
B) A database connection error
C) A session timeout
D) An invalid HTTP request
**Answer:** A) A generic error in the servlet execution

66. Which interface provides methods to manage HTTP sessions?


A) HttpSession
B) ServletContext
C) ServletRequest
D) HttpServletRequest
**Answer:** A) HttpSession
67. What is the use of `response.setCharacterEncoding()`?
A) To specify the encoding for the request
B) To define the character encoding for the response body
C) To set the session encoding
D) To configure the servlet
**Answer:** B) To define the character encoding for the response body

68. What does the `getServletName()` method return?


A) The name of the servlet as defined in the web.xml
B) The class name of the servlet
C) The request parameter name
D) The context path of the servlet
**Answer:** A) The name of the servlet as defined in the web.xml

69. Which of the following is true about the `doOptions()` method?


A) It is called to handle HTTP OPTIONS requests.
B) It is used to retrieve data from the server.
C) It is not implemented in servlets.
D) It is a deprecated method.
**Answer:** A) It is called to handle HTTP OPTIONS requests.

70. How can servlets be made reusable across different applications?


A) By using a custom servlet container
B) By implementing the Singleton pattern
C) By defining them in a JAR file
D) By using application context
**Answer:** C) By defining them in a JAR file

71. What is the role of the `@WebServlet` annotation?


A) To define a servlet in the deployment descriptor
B) To specify servlet mapping and configuration
C) To provide a default servlet implementation
D) To configure the session management
**Answer:** B) To specify servlet mapping and configuration

72. Which method would you use to read a cookie in a servlet?


A) request.getCookies()
B) request.readCookie()
C) request.getCookie()
D) request.fetchCookie()
**Answer:** A) request.getCookies()

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

75. What happens if a servlet fails to initialize?


A) The servlet is automatically reloaded.
B) The server throws a ServletException.
C) The servlet is ignored by the server.
D) The servlet continues to run.
**Answer:** B) The server throws a ServletException.

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

77. What is the result of calling `response.setHeader("Cache-Control", "no-


cache")`?
A) It enables caching.
B) It disables caching.
C) It allows caching for one hour.
D) It configures the server to handle requests asynchronously.
**Answer:** B) It disables caching.

78. Which method is used to retrieve all session attributes in a servlet?


A) session.getAllAttributes()
B) session.getAttributeNames()
C) session.getAttributeList()
D) session.getAttributes()
**Answer:** B) session.getAttributeNames()

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

80. What is the main advantage of using `ServletConfig`?


A) It manages session data.
B) It provides configuration information to the servlet.
C) It handles request parameters.
D) It stores response data.
**Answer:** B) It provides configuration information to the servlet

81. What is the main function of the `HttpServlet` class?


A) To handle only HTTP GET requests
B) To provide a framework for handling HTTP requests and responses
C) To manage database connections
D) To serve static files

**Answer:** B) To provide a framework for handling HTTP requests and responses

82. Which method is called when a servlet needs to handle an error?


A) handleError()
B) doError()
C) service()
D) init()

**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:** A) To write data to the client in text format

84. Which of the following best describes the `HttpSession` object?


A) It stores data only for the duration of a single request.
B) It maintains state across multiple requests from the same client.
C) It is used to send responses to clients.
D) It is responsible for creating new servlets.

**Answer:** B) It maintains state across multiple requests from the same client.

85. What does the `request.getMethod()` method return?


A) The request headers
B) The request URL
C) The HTTP method used for the request (GET, POST, etc.)
D) The client’s IP address

**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

87. What is a `ServletContextListener` used for?


A) To listen for HTTP requests
B) To perform actions during the servlet context lifecycle
C) To manage session attributes
D) To handle user authentication

**Answer:** B) To perform actions during the servlet context lifecycle

88. What is the function of the `setContentType()` method in a servlet?


A) To specify the character encoding for the response
B) To define the MIME type of the response
C) To set a cookie in the response
D) To redirect the client to another URL

**Answer:** B) 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?
A) @WebServlet
B) @ServletMapping
C) @URLMapping
D) @RequestMapping

**Answer:** A) @WebServlet

90. What is the main advantage of using servlets over CGI?


A) Servlets are slower than CGI.
B) Servlets can handle multiple requests simultaneously.
C) Servlets require more server resources than CGI.
D) CGI scripts are easier to write than servlets.

**Answer:** B) Servlets can handle multiple requests simultaneously.

91. What is the purpose of the `HttpServletRequest.getSession()` method?


A) To create a new session
B) To return the current session associated with the request
C) To invalidate the current session
D) To retrieve request parameters

**Answer:** B) To return the current session associated with the request

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) Text, binary, and multimedia data

94. What does the `request.getRequestDispatcher("/path")` method return?


A) A URL to redirect to
B) An object that can forward or include a response
C) The content of the specified resource
D) A new HTTP session

**Answer:** B) An object that can forward or include a response

95. Which HTTP status code indicates a successful request?


A) 404
B) 500
C) 200
D) 301

**Answer:** C) 200

96. What is the function of a `Filter` in a servlet application?


A) To serve static files
B) To intercept requests and responses for preprocessing or postprocessing
C) To define servlet initialization parameters
D) To manage database connections

**Answer:** B) To intercept requests and responses for preprocessing or


postprocessing

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()

98. What does the `response.setHeader("Location", "url")` method accomplish?


A) It sends a cookie to the client
B) It specifies the URL to which the client should be redirected
C) It sets the content type of the response
D) It configures the servlet context

**Answer:** B) 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?
A) service()
B) handle()
C) default()
D) doRequest()

**Answer:** A) service()

100. What happens if you call `response.sendError(404)` in a servlet?


A) The server redirects the request to a different page.
B) A custom error page is displayed with the 404 status.
C) The response is sent without an error message.
D) The server crashes.

**Answer:** B) A custom error page is displayed with the 404 status.

101. What is a cookie in the context of web applications?


A) A small piece of server-side data
B) A small piece of client-side data stored in the browser
C) A type of database storage
D) A security protocol for web applications

**Answer:** B) A small piece of client-side data stored in the browser

102. How do you create a cookie in a servlet?


A) Cookie cookie = new Cookie();
B) Cookie cookie = new Cookie(name, value);
C) Cookie cookie = new Cookie(name);
D) Cookie cookie = new Cookie(value);

**Answer:** B) Cookie cookie = new Cookie(name, value);

103. Which method is used to add a cookie to the response in a servlet?


A) response.addCookie(cookie)
B) response.setCookie(cookie)
C) response.sendCookie(cookie)
D) response.includeCookie(cookie)

**Answer:** A) response.addCookie(cookie)

104. What is the primary purpose of session tracking in web applications?


A) To store data for static resources
B) To maintain user-specific data across multiple requests
C) To improve database performance
D) To enhance security by encrypting requests
**Answer:** B) To maintain user-specific data across multiple requests

105. How can you invalidate a session in a servlet?


A) session.remove()
B) session.clear()
C) session.invalidate()
D) session.destroy()

**Answer:** C) session.invalidate()

106. Which method retrieves a cookie from the request in a servlet?


A) request.getCookie()
B) request.getCookies()
C) request.getAllCookies()
D) request.retrieveCookies()

**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) 4096 bytes

108. Which of the following statements about session attributes is true?


A) Session attributes are shared across all sessions.
B) Session attributes are limited to a single request.
C) Session attributes persist for the duration of the session.
D) Session attributes cannot be modified after creation.

**Answer:** C) Session attributes persist for the duration of the session.

109. Which class is used to manage sessions in Java Servlets?


A) HttpSession
B) ServletSession
C) SessionManager
D) CookieSession

**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.

**Answer:** C) Each cookie can be up to 4 KB in size.

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:** A) They are accessible from any path on the server.

114. How do you create a new session in a servlet?


A) request.createSession()
B) request.getSession(true)
C) response.newSession()
D) session.create()

**Answer:** B) request.getSession(true)

115. What happens if you call `request.getSession(false)` when there is no current


session?
A) It creates a new session.
B) It returns null.
C) It throws an exception.
D) It returns an empty session.

**Answer:** B) It returns null.

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)

117. What is the primary use of the `HttpSession` interface?


A) To store cookies
B) To manage request parameters
C) To track user sessions and store user-specific data
D) To configure servlets

**Answer:** C) To track user sessions and store user-specific data

118. Which of the following is a potential security issue with cookies?


A) They are always encrypted.
B) They can be easily manipulated by users.
C) They can only store simple data types.
D) They are automatically deleted after a session ends.

**Answer:** B) They can be easily manipulated by users.

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)

120. What is the typical lifetime of a session created in a servlet?


A) Until the server is restarted
B) Until the browser is closed
C) Until the user explicitly logs out or the session is invalidated
D) It is fixed at 30 minutes

**Answer:** C) Until the user explicitly logs out or the session is invalidated

121. What method is used to set a cookie's expiration date?


A) cookie.setExpiry(date)
B) cookie.setMaxAge(seconds)
C) cookie.setExpires(date)
D) cookie.setExpiration(seconds)

**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

123. What is the purpose of the `HttpSession` interface's `setAttribute(String


name, Object value)` method?
A) To store a cookie
B) To save user-specific data during a session
C) To set session timeout
D) To create a new session

**Answer:** B) To save user-specific data during a session

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.

**Answer:** B) Set the cookie's HttpOnly attribute to true

125. What is the purpose of the `response.setHeader("Set-Cookie", cookie)` method?


A) To retrieve cookies from the client
B) To create or update a cookie in the client's browser
C) To delete a cookie
D) To log cookie usage

**Answer:** B) 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?
A) session.getId()
B) session.getSessionId()
C) request.getSessionId()
D) request.getId()

**Answer:** A) session.getId()

127. What is the result of calling `response.sendRedirect("url")` after adding a


cookie?
A) The cookie will not be sent to the client.
B) The cookie will be sent as part of the redirect response.
C) Cookies cannot be sent with redirect responses.
D) The cookie will be lost.

**Answer:** B) The cookie will be sent as part of the redirect response.

128. Which of the following is true about the `HttpSession` object?


A) It is created for each request.
B) It can be accessed only by a single user.
C) It is not thread-safe.
D) It can store objects of any type.

**Answer:** D) It can store objects of any type.

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) They are deleted.

130. What attribute must be set to `true` to restrict a cookie to secure


connections (HTTPS)?
A) cookie.setSecure(true)
B) cookie.setHttpOnly(true)
C) cookie.setSecureOnly(true)
D) cookie.setProtection(true)

**Answer:** A) cookie.setSecure(true)

131. What does the `HttpServletRequest.getParameter(String name)` method return?


A) The value of a specific parameter sent with the request
B) All parameters as a map
C) The request URL
D) The client's IP address

**Answer:** A) The value of a specific parameter sent with the request

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)

133. What type of data is typically sent in a URL query string?


A) Binary data
B) Text data
C) Encrypted data
D) JSON data

**Answer:** B) Text data

134. How can you read data sent in a multipart/form-data request?


A) Using HttpServletRequest methods
B) Using a dedicated library like Apache Commons FileUpload
C) Using the response object
D) It cannot be read in a servlet.

**Answer:** B) Using a dedicated library like Apache Commons FileUpload

135. What is the role of the `doPut()` method in a servlet?


A) To handle GET requests
B) To handle PUT requests
C) To handle all HTTP methods
D) To handle only POST requests

**Answer:** B) To handle PUT requests

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

137. Which interface must be implemented to create a filter in a servlet


application?
A) HttpServlet
B) Filter
C) ServletConfig
D) ServletContext

**Answer:** B) Filter

138. What is the purpose of the `init()` method in a servlet?


A) To handle HTTP requests
B) To initialize the servlet and its resources
C) To destroy the servlet
D) To send a response

**Answer:** B) To initialize the servlet and its resources

139. How can you obtain the client’s IP address in a servlet?


A) request.getRemoteAddr()
B) request.getClientIP()
C) request.getAddress()
D) request.getRemoteHost()

**Answer:** A) request.getRemoteAddr()

140. What does the `response.setContentType("text/html")` method do?


A) It sets the character encoding of the response
B) It defines the MIME type of the response
C) It sets the status code of the response
D) It redirects the client to another URL

**Answer:** B) It defines the MIME type of the response

141. What is the role of the `doDelete()` method in a servlet?


A) To handle DELETE requests
B) To handle POST requests
C) To handle GET requests
D) To handle all HTTP methods

**Answer:** A) To handle DELETE requests

142. Which annotation is used to define a servlet's initialization parameters?


A) @WebInitParam
B) @InitParam
C) @ServletParam
D) @Param

**Answer:** A) @WebInitParam

143. What does the `HttpServletRequest.getHeader(String name)` method return?


A) The request body
B) The value of the specified request header
C) All headers as a map
D) The request URL

**Answer:** B) The value of the specified request header

144. Which method is used to retrieve all session attributes in a servlet?


A) session.getAttributes()
B) session.getAttributeNames()
C) session.getAllAttributes()
D) session.getValues()

**Answer:** B) session.getAttributeNames()

145. What is the purpose of the `destroy()` method in a servlet?


A) To initialize resources
B) To clean up resources before the servlet is destroyed
C) To handle requests
D) To manage session attributes

**Answer:** B) To clean up resources before the servlet is destroyed

146. How do you create a URL pattern for a servlet?


A) By using a URL mapping in web.xml
B) By implementing the doGet() method
C) By using the @WebServlet annotation
D) Both A and C

**Answer:** D) Both A and C

147. What does the `response.getOutputStream()` method return?


A) A writer for sending text data
B) An output stream for binary data
C) A stream for reading input
D) The response headers

**Answer:** B) An output stream for binary data


148. How can you forward a request to another resource in a servlet?
A) response.forward("/path")
B) request.forward("/path")
C) request.getRequestDispatcher("/path").forward(request, response)
D) response.sendRedirect("/path")

**Answer:** C) request.getRequestDispatcher("/path").forward(request, response)

149. What is the default scope of session attributes?


A) Request
B) Application
C) Session
D) Cookie

**Answer:** C) Session

150. Which HTTP method is generally used to submit data to a server?


A) GET
B) POST
C) PUT
D) DELETE

**Answer:** B) POST

151. How do you retrieve the context parameter defined in web.xml?


A) servletConfig.getInitParameter(name)
B) context.getInitParameter(name)
C) request.getContextParameter(name)
D) servletContext.getInitParameter(name)

**Answer:** D) servletContext.getInitParameter(name)

152. What is the purpose of the `setCharacterEncoding()` method in a servlet?


A) To set the character encoding for the response
B) To set the character encoding for the request
C) To define the MIME type of the response
D) To set the request method

**Answer:** B) To set the character encoding for the request

153. What is the main benefit of using a servlet filter?


A) To handle errors
B) To perform preprocessing and postprocessing on requests and responses
C) To manage session attributes
D) To define servlet initialization parameters

**Answer:** B) 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?
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

**Answer:** A) To retrieve the servlet's initialization parameters

156. Which of the following is true about servlet lifecycle methods?


A) init() is called after destroy()
B) service() is called for each request
C) destroy() is called before init()
D) service() is optional

**Answer:** B) service() is called for each request

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>

158. What does the `request.getContentType()` method return?


A) The request headers
B) The MIME type of the request body
C) The request method
D) The client’s IP address

**Answer:** B) The MIME type of the request body

159. Which class provides methods to read incoming request data in a servlet?
A) ServletResponse
B) HttpServletRequest
C) ServletConfig
D) HttpServlet

**Answer:** B) HttpServletRequest

160. What is the default session timeout value in a servlet container?


A) 30 minutes
B) 15 minutes
C) 60 minutes
D) 1 hour

**Answer:** B) 15 minutes

You might also like