Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
45 views38 pages

Unit 6

This document provides questions and answers about servlets and the servlet life cycle. It discusses the purpose and order of methods like init(), service(), doGet(), and destroy(). Key topics covered include handling HTTP requests, initializing servlets, and releasing resources.

Uploaded by

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

Unit 6

This document provides questions and answers about servlets and the servlet life cycle. It discusses the purpose and order of methods like init(), service(), doGet(), and destroy(). Key topics covered include handling HTTP requests, initializing servlets, and releasing resources.

Uploaded by

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

UNIT 6

201. Which method is called by the container to indicate to a servlet that it should initialize?
A) `init()`
B) `service()`
C) `doGet()`
D) `destroy()`

Answer: A) `init()`

202. What does the `init()` method do in the servlet life cycle?
A) Handles incoming client requests
B) Initializes the servlet
C) Sends response to the client
D) Destroys the servlet

Answer: B) Initializes the servlet

203. Which method is invoked every time a request is received by the servlet?
A) `init()`
B) `service()`
C) `doPost()`
D) `destroy()`

Answer: B) `service()`

204. The `service()` method in a servlet:


A) Processes HTTP requests
B) Initializes the servlet
C) Sends responses to the client
D) Handles servlet destruction

Answer: A) Processes HTTP requests

pg. 1
UNIT 6

205. Which HTTP method is handled by the `doGet()` method in a servlet?


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

Answer: B) GET

6. The `doPost()` method in a servlet is used to:


A) Handle HTTP POST requests
B) Initialize the servlet
C) Send responses to the client
D) Handle servlet destruction

Answer: A) Handle HTTP POST requests

7. What is the purpose of the `destroy()` method in the servlet life cycle?
A) Initialize the servlet
B) Handle incoming requests
C) Release resources and perform cleanup
D) Send responses to the client

Answer: C) Release resources and perform cleanup


8. When does the `destroy()` method of a servlet get called by the container?
A) After every request
B) Before processing a request
C) During servlet initialization
D) Before the servlet is removed from service

Answer: D) Before the servlet is removed from service

pg. 2
UNIT 6

9. Which method should be overridden to handle initialization tasks in a servlet?


A) `init()`
B) `service()`
C) `doGet()`
D) `destroy()`

Answer: A) `init()`

10. In which order do the methods of a servlet get called during its life cycle?
A) `init() -> service() -> destroy()`
B) `service() -> init() -> destroy()`
C) `init() -> destroy() -> service()`
D) `init() -> service() -> destroy()`

Answer: D) `init() -> service() -> destroy()`

11. What does the `getServletContext()` method return in a servlet?


A) Current session context
B) ServletConfig object
C) ServletContext object
D) HttpServlet object

Answer: C) ServletContext object

A) `getInitParameter()`
B) `init()`
C) `getServletContext()`
D) `service()`

pg. 3
UNIT 6

Answer: A) `getInitParameter()`

13. Which method is used to send a redirect response to the client in a servlet?
A) `sendRedirect()`
B) `forward()`
C) `redirect()`
D) `sendResponse()`

Answer: A) `sendRedirect()`

14. Which class must a servlet extend to handle HTTP requests and responses?
A) `Servlet`
B) `HttpServlet`
C) `GenericServlet`
D) `WebServlet`

Answer: B) `HttpServlet`

15. What is the default HTTP method handled by the `service()` method in a servlet?
A) GET
B) POST
C) DELETE
D) PUT
Answer: A) GET
16. Which method is used to retrieve the client's request information, such as parameters,
headers, and cookies?
A) `getRequestHeader()`
B) `getRequestParameters()`
C) `getRequest()`
D) `HttpServletRequest()`

pg. 4
UNIT 6

Answer: D) `HttpServletRequest()`

17. Which method is used to set the content type of the response in a servlet?
A) `setContentType()`
B) `setResponseType()`
C) `setResponseContentType()`
D) `setHeader()`

Answer: A) `setContentType()`

18. What is the purpose of the `getServletConfig()` method in a servlet?


A) Retrieves the initialization parameters
B) Initializes the servlet
C) Retrieves the servlet configuration
D) Releases resources
Answer: C) Retrieves the servlet configuration

19. Which interface needs to be implemented to create a servlet in Java?


A) `Servlet`
B) `HttpServlet`
C) `ServletConfig`
D) `ServletRequest`

Answer: A) `Servlet`

A) Defines the servlet mappings and configurations


B) Handles HTTP requests and responses
C) Contains HTML code for the web pages
D) Manages session information

Answer: A) Defines the servlet mappings and configurations

pg. 5
UNIT 6

1. Which method is responsible for handling a servlet's destruction by the container?


A) `finalize()`
B) `destroy()`
C) `dispose()`
D) `end()`

Answer: B) `destroy()`

2. In the servlet life cycle, when is the `init()` method called?


A) After every request
B) Before handling the first request
C) When the servlet is destroyed
D) During servlet reconfiguration
Answer: B) Before handling the first request

3. What happens when a servlet is initialized more than once?


A) It causes a compilation error
B) It throws a `ServletException`
C) Each initialization is ignored
D) Only the first initialization is execute
Answer: D) Only the first initialization is executed
4. The `service()` method in a servlet:
A) Handles HTTP requests
B) Initializes the servlet
C) Sends responses to the client
D) Handles servlet destruction

Answer: A) Handles HTTP requests

5. Which method should be overridden to handle HTTP DELETE requests in a servlet?


A) `doDelete()`
B) `delete()`

pg. 6
UNIT 6

C) `doGet()`
D) `service()`

Answer: A) `doDelete()`

6. What is the role of the `getLastModified()` method in a servlet?


A) Returns the time of the last request
B) Indicates the time when the servlet was last modified
C) Handles the modification of response headers
D) Determines the session's last modification time

Answer: B) Indicates the time when the servlet was last modified

7. The `doOptions()` method in a servlet is used to handle:


A) HTTP OPTIONS requests
B) Servlet configuration options
C) HTTP GET requests
D) Servlet initialization options

Answer: A) HTTP OPTIONS requests

8. Which method is used to obtain the name of the servlet configuration?


A) `getName()`
B) `getServletName()`
C) `getConfigurationName()`
D) `retrieveName()`

Answer: B) `getServletName()`

9. What is the purpose of the `getServletInfo()` method in a servlet?


A) Provides information about the servlet's initialization
B) Retrieves information about the servlet container

pg. 7
UNIT 6

C) Returns descriptive information about the servlet


D) Determines the last modification time of the servlet

Answer: C) Returns descriptive information about the servlet

10. Which method should be used to handle the HEAD HTTP method in a servlet?
A) `doHead()`
B) `head()`
C) `doGet()`
D) `service()`

Answer: A) `doHead()`

11. What is the significance of the `ServletContext` in a servlet?


A) Manages servlet's session information
B) Provides access to the servlet's configuration
C) Handles client requests and responses
D) Manages the servlet's life cycle
Answer: B) Provides access to the servlet's configuration

12. The `doTrace()` method in a servlet is primarily used for:


A) Tracking servlet initialization
B) Tracing the client's request path
C) Sending response headers
D) Servlet cleanup operations

Answer: B) Tracing the client's request path

13. Which method is used to retrieve the MIME type of a file in a servlet?
A) `getMimeType()`
B) `getContentType()`
C) `getFileMimeType()`

pg. 8
UNIT 6

D) `getMIMEType()`

Answer: B) `getContentType()`

14. What does the `getInitParameterNames()` method in `ServletConfig` return?


A) Servlet's initialization parameter values
B) Names of the servlet's configuration parameters
C) Names of the servlet's initialization parameters
D) Servlet's context parameters
Answer: C) Names of the servlet's initialization parameters

15. Which method is used to set the status code for the HTTP response in a servlet?
A) `setStatus()`
B) `setStatusCode()`
C) `sendError()`
D) `setResponseCode()`

Answer: A) `setStatus()`

16. The `getRemoteAddr()` method in `HttpServletRequest` retrieves:


A) The client's IP address
B) The server's IP address
C) The servlet's IP address
D) The session's IP address

Answer: A) The client's IP address

17. What is the purpose of the `getServerInfo()` method in `ServletContext`?


A) Retrieves information about the client's server
B) Provides details about the servlet container
C) Retrieves the server's IP address
D) Returns the servlet's initialization parameters

pg. 9
UNIT 6

Answer: B) Provides details about the servlet container

18. Which method is invoked by the container to allow a servlet to clean up any resources it
holds?
A) `dispose()`
B) `cleanup()`
C) `destroy()`
D) `release()`
Answer: C) `destroy()`

19. What does the `setInitParameter()` method in `ServletContext` do?


A) Sets the servlet's initialization parameters
B) Configures the servlet container parameters
C) Initializes the servlet context
D) Sets the servlet's configuration parameters

Answer: A) Sets the servlet's initialization parameters

20. Which method should be overridden to handle asynchronous processing in a servlet?


A) `asyncService()`
B) `doAsync()`
C) `doOptions()`
D) `doAsyncProcess()`

Answer: A) `asyncService()

1. Which package contains the Servlet API classes in Java?


A) `java.servlet`
B) `javax.servlet`
C) `java.servlets`
D) `javax.servlets`

pg. 10
UNIT 6

Answer: B) `javax.servlet`

2. The `Servlet` interface is located in which package?


A) `javax.servlet`
B) `java.servlet`
C) `javax.servlets`
D) `java.servlets`
Answer: A) `javax.servlet`

3. What is the purpose of the `ServletRequest` interface?


A) Handles client requests
B) Sends responses to the client
C) Provides data including parameter names and values
D) Initializes servlets

Answer: C) Provides data including parameter names and values

4. Which method is used to retrieve the value of a parameter from a `ServletRequest` object?
A) `getParameterValue()`
B) `getParameter()`
C) `retrieveParameter()`
D) `fetchParameter()`

Answer: B) `getParameter()`

5. The `ServletResponse` interface is used for:


A) Handling client requests
B) Processing servlet initialization
C) Sending responses to the client
D) Retrieving servlet context information
Answer: C) Sending responses to the client

pg. 11
UNIT 6

6. Which method in the `ServletResponse` interface is used to set the content type of the
response?
A) `setContentType()`
B) `setContent()`
C) `setResponseContentType()`
D) `setHeader()`
Answer: A) `setContentType()`

7. The `ServletConfig` interface is used to provide configuration information to a servlet from:


A) The web server
B) The client's browser
C) The deployment descriptor
D) The servlet container

Answer: C) The deployment descriptor

8. Which method is used to retrieve an initialization parameter from the `ServletConfig` object?
A) `getInitParameter()`
B) `retrieveParameter()`
C) `getParameter()`
D) `fetchInitParameter()`

Answer: A) `getInitParameter()`

9. The `ServletContext` interface represents:


A) Configuration information for a single servlet
B) Configuration information for the entire web application
C) Request parameters sent by the client
D) Response data sent to the client

Answer: B) Configuration information for the entire web application

pg. 12
UNIT 6

10. Which method is used to obtain the MIME type of a file in the `ServletContext`?
A) `getMIMEType()`
B) `retrieveMimeType()`
C) `getContentType()`
D) `getMimeType()`
Answer: D) `getMimeType()`
A) Implementation for the `service()` method
B) Implementation for the `doGet()` method
C) Implementation for the `init()` method
D) Implementation for the `destroy()` method

Answer: A) Implementation for the `service()` method A) `doGet()`


B) `doPost()`
C) `service()`
D) `init()`

Answer: C) `service()`

13. What is the role of the `getServletConfig()` method in `GenericServlet`?


A) Retrieves the servlet's initialization parameters
B) Initializes the servlet
C) Retrieves the servlet configuration
D) Releases resources

Answer: C) Retrieves the servlet configuration

14. Which method is used to log messages in a servlet using `GenericServlet`?


A) `log()`
B) `writeLog()`
C) `recordLog()`
D) `printLog()`

pg. 13
UNIT 6

Answer: A) `log()`

15. The `getServletInfo()` method in `GenericServlet` is used to:


A) Retrieve servlet initialization information
B) Get the servlet's configuration details
C) Obtain information about the servlet
D) Set the servlet's information

Answer: C) Obtain information about the servlet A) Parameter names in a comma-separated


string
B) Parameter values in an array
C) Enumeration of parameter names
D) The number of parameters

Answer: C) Enumeration of parameter names

17. Which method in `ServletResponse` is used to set the character encoding of the response?
A) `setCharset()`
B) `setCharacterEncoding()`
C) `encodeCharset()`
D) `setResponseEncoding()`

Answer: B) `setCharacterEncoding()`

18. The `ServletContext` interface allows a servlet to:


A) Communicate with other servlets
B) Store session information
C) Share data with other servlets in the application
D) Handle HTTP requests and responses

Answer: C) Share data with other servlets in the application

pg. 14
UNIT 6

19. What does the `log()` method in `GenericServlet` do if no log messages are specified?
A) Logs a default message
B) Throws a `NullPointerException`
C) Logs an empty message
D) Does not perform any action

Answer: C) Logs an empty message A) Servlet's session context


B) ServletConfig object
C) ServletContext object
D) HttpServlet object

Answer: C) ServletContext object

21. The `ServletRequest` method `getProtocol()` returns:


A) The protocol used for the request
B) The server's protocol
C) The client's protocol
D) The servlet's protocol

Answer: A) The protocol used for the request

22. Which method in `ServletResponse` is used to set headers for the response?
A) `setHeaders()`
B) `addHeader()`
C) `setResponseHeaders()`
D) `addResponseHeader()`

Answer: D) `addResponseHeader()`

23. The `ServletRequest` method `getLocalAddr()` retrieves:


A) The client's IP address

pg. 15
UNIT 6

B) The server's IP address


C) The servlet's IP address
D) The local machine's IP address

Answer: D) The local machine's IP address A) `sendError()`


B) `errorResponse()`
C) `setError()`
D) `responseError()`
Answer: A) `sendError()`

25. The `ServletRequest` method `getServerName()` retrieves:

A) The client's server name


B) The server's IP address
C) The server's name
D) The servlet's server name

Answer: C) The server's name

26. Which method in `ServletResponse` is used to set the status code for the response?
A) `setStatus()`
B) `setStatusCode()`
C) `sendError()`
D) `setResponseCode()`
Answer: A) `setStatus()`

27. The `ServletContext` method `setAttribute()` is used to:


A) Get attributes from the servlet context
B) Set attributes in the servlet context
C) Remove attributes from the servlet context
D) Retrieve servlet context parameters

pg. 16
UNIT 6

Answer: B) Set attributes in the servlet context

28. Which method in `ServletRequest` is used to retrieve the input stream for reading binary
data?
A) `getInputStream()`
B) `readStream()`
C) `retrieveInputStream()`
D) `inputStream()`

Answer: A) `getInputStream()`

29. The `ServletResponse` method `getCharacterEncoding()` returns:


A) The character encoding used for the response
B) The server's character encoding
C) The client's character encoding
D) The servlet's character encoding

Answer: A) The character encoding used for the response

30. Which method in `ServletContext` is used to retrieve the real path for a given virtual path?
A) `getRealPath()`
B) `retrievePath()`
C) `getPath()`
D) `realPath()`
Answer: A) `getRealPath()`

31. The `ServletRequest` method `getParameterValues()` retrieves:


A) All parameter values for a specified parameter name
B) A single parameter value
C) Parameter names and their values
D) The number of parameters

pg. 17
UNIT 6

Answer: A) All parameter values for a specified parameter name

32. Which method in `ServletResponse` is used to write a character string to the response?
A) `writeString()`
B) `printString()`
C) `sendString()`
D) `writer()`

Answer: D) `writer()`

33. The `ServletRequest` method `getRemoteHost()` retrieves:


A) The client's IP address
B) The server's IP address
C) The remote host name
D) The local machine's host name

Answer: C) The remote host name

34. Which method in `ServletResponse` is used to set the length of the content in the response?
A) `setContentLength()`
B) `setLength()`
C) `setResponseLength()`
D) `setContentSize()`
Answer: A) `setContentLength()`

35. The `ServletContext` method `getInitParameter()` retrieves:


A) Servlet's initialization parameters
B) Context parameters
C) Servlet's configuration details
D) Response data

pg. 18
UNIT 6

Answer: B) Context parameters

36. Which method in `ServletResponse` is used to clear the content of the response?
A) `clearContent()`
B) `removeContent()`
C) `reset()`
D) `clearResponse()`

Answer: C) `reset()`

37. The `ServletRequest` method `getServerPort()` retrieves:


A) The client's port number
B) The server's port number
C) The servlet's port number
D) The local machine's port number

Answer: B) The server's port number

38. Which method in `ServletResponse` is used to obtain the output stream for writing binary
data?
A) `getOutputStream()`
B) `writeStream()`
C) `retrieveOutputStream()`
D) `outputStream()`

Answer: A) `getOutputStream()` A) Server's IP address


B) Servlet's server information
C) Server's name and version
D) Servlet's configuration details
Answer: C) Server's name and version A) `getParameterNames()`
B) `retrieveParameterNames()`
C) `getAllParameterNames()`

pg. 19
UNIT 6

D) `fetchParameterNames()`

Answer: A) `getParameterNames()`

1. Which package contains the HTTP servlet-specific extensions of the `javax.servlet` package in
Java?
A) `javax.servlet.http`
B) `java.servlet.http`
C) `java.servlets`
D) `javax.servlets`

Answer: A) `javax.servlet.http`

2. The `HttpServletRequest` interface represents:


A) A request sent by a client to the server
B) A response sent by the server to the client
C) Server configuration details
D) Servlet initialization parameters
Answer: A) A request sent by a client to the server
A) `getMethod()`
B) `getRequestMethod()`
C) `retrieveMethod()`
D) `fetchRequestMethod()`

Answer: A) `getMethod()`

4. The `HttpServletResponse` interface is used to:


A) Handle client requests
B) Send responses to the client
C) Maintain session information
D) Access server configuration

pg. 20
UNIT 6

Answer: B) Send responses to the client

5. Which method in `HttpServletResponse` is used to set the status code for the response?
A) `setStatus()`
B) `setStatusCode()`
C) `sendError()`
D) `setResponseCode()`

Answer: A) `setStatus()`

6. The `HttpSession` interface represents:


A) A single HTTP request
B) A single HTTP response
C) An object representing a user session
D) Servlet configuration information
Answer: C) An object representing a user session

7. Which method in `HttpSession` is used to retrieve an attribute stored in the session?


A) `getAttribute()`
B) `retrieveAttribute()`
C) `getSessionAttribute()`
D) `fetchAttribute()`

Answer: A) `getAttribute()`
8. The `Cookie` class is used to:
A) Maintain user session data
B) Store data on the client side
C) Retrieve server configuration details
D) Handle HTTP request parameters

Answer: B) Store data on the client side

pg. 21
UNIT 6

9. Which method in `Cookie` is used to set the maximum age of the cookie in seconds?
A) `setAge()`
B) `setMaxAge()`
C) `expire()`
D) `setExpiration()`

Answer: B) `setMaxAge()`

10. The `HttpServlet` class is an abstract class that provides:


A) Implementation for the `service()` method
B) Implementation for the `doGet()` method
C) Implementation for the `init()` method
D) Implementation for the `destroy()` method

Answer: A) Implementation for the `service()` method

11. Which method should be overridden in `HttpServlet` to handle HTTP GET requests?
A) `doGet()`
B) `doPost()`
C) `service()`
D) `init()`

Answer: A) `doGet()` A) An event fired when a session is created


B) An event fired when a session is invalidated
C) An event fired when a session attribute is changed
D) An event fired when a session times out
Answer: A) An event fired when a session is created

13. Which method in `HttpSessionEvent` is used to retrieve the session associated with the
event?
A) `getSession()`
B) `retrieveSession()`

pg. 22
UNIT 6

C) `getEventSession()`
D) `fetchSession()`

Answer: A) `getSession()`

14. The `HttpSessionBindingEvent` class represents:


A) An event fired when a session is created
B) An event fired when a session is invalidated
C) An event fired when an object is added or removed from a session
D) An event fired when a session attribute is changed

Answer: C) An event fired when an object is added or removed from a session

15. Which method in `HttpSessionBindingEvent` is used to retrieve the name of the attribute
associated with the event?
A) `getAttributeName()`
B) `retrieveAttributeName()`
C) `getName()`
D) `fetchAttributeName()`

Answer: A) `getAttributeName()` A) The client's HTTP method used in the request


B) The server's HTTP method
C) The servlet's HTTP method
D) The request protocol

Answer: A) The client's HTTP method used in the request

17. Which method in `HttpServletResponse` is used to set a header in the response?


A) `setHeader()`
B) `addHeader()`
C) `setResponseHeader()`
D) `addResponseHeader()`

pg. 23
UNIT 6

Answer: D) `addResponseHeader()`

18. The `HttpSession` method `setMaxInactiveInterval()` sets:


A) The maximum number of sessions allowed
B) The session's maximum active time in seconds
C) The session's expiration time in milliseconds
D) The session's creation time
Answer: B) The session's maximum active time in seconds

19. Which method in `HttpServletRequest` is used to retrieve the value of a header from the
request?
A) `getHeader()`
B) `retrieveHeader()`
C) `getRequestHeader()`
D) `fetchHeader()`

Answer: A) `getHeader()` A) Set the content type of the response


B) Get the content type of the request
C) Define the servlet's content type
D) Set the character encoding of the response

Answer: A) Set the content type of the response

21. The `HttpSession` method `getAttributeNames()` returns:


A) All session attributes as a string
B) Enumeration of attribute names
C) All session attributes as an array
D) The number of session attributes

Answer: B) Enumeration of attribute names

pg. 24
UNIT 6

22. Which method in `Cookie` is used to set the cookie's domain?


A) `setDomain()`
B) `setCookieDomain()`
C) `setDomainName()`
D) `setCookieDomainName()`

Answer: A) `setDomain()`

23. The `HttpServlet` method `doPost()` is used to handle:


A) HTTP GET requests
B) HTTP POST requests
C) Session creation events
D) Session invalidation events

Answer: B) HTTP POST requests

24. Which method in `HttpSessionEvent` is used to retrieve the session being invalidated?
A) `getSession()`
B) `retrieveSession()`
C) `getEventSession()`
D) `fetchSession()`

Answer: A) `getSession()`

25. The `HttpServletRequest` method `getQueryString()` returns:


A) The query string appended to the request URL
B) The client's IP address
C) The server's domain name
D) The request method used

Answer: A) The query string appended to the request URL

pg. 25
UNIT 6

26. Which method in `HttpServletResponse` is used to set the character encoding of the
response?
A) `setCharset()`
B) `setCharacterEncoding()`
C) `encodeCharset()`
D) `setResponseEncoding()`

Answer: B) `setCharacterEncoding()`

27. The `HttpSession` method `invalidate()` is used to:


A) Set the session's expiration time
B) Remove the session's attributes
C) Get the session's ID
D) Create a new session
Answer: B) Remove the session's attributes

28. Which method in `Cookie` is used to set the path on the server where the cookie is available?
A) `setPath()`
B) `setCookiePath()`
C) `setServerPath()`
D) `setCookieServerPath()`

Answer: A) `setPath()`

29. The `HttpServlet` method `init()` is called by the servlet container to:
A) Handle client requests
B) Initialize the servlet
C) Send responses to the client
D) Manage session information

Answer: B) Initialize the servlet

pg. 26
UNIT 6

30. Which method in `HttpServletRequest` is used to retrieve the request URI?


A) `getRequestURI()`
B) `retrieveRequestURI()`
C) `getURI()`
D) `fetchRequestURI()`

Answer: A) `getRequestURI()`

31. The `HttpServletResponse` method `sendRedirect()` is used to:


A) Forward the request to another servlet
B) Invalidate the session
C) Send a response to the client with a new URL
D) Set a new session ID
Answer: C) Send a response to the client with a new URL

32. Which method in `HttpSessionEvent` is used to check if the session is a new session?
A) `isNewSession()`
B) `isSessionNew()`
C) `getSessionIsNew()`
D) `fetchIsNewSession()`

Answer: A) `isNewSession()`

33. The `HttpServletRequest` method `getPathInfo()` returns:


A) The server's path
B) The servlet's path
C) The extra path information associated with the URL
D) The client's path

Answer: C) The extra path information associated with the URL

34. Which method in `HttpServletResponse` is used to add a cookie to the response?

pg. 27
UNIT 6

A) `addCookie()`
B) `setCookie()`
C) `appendCookie()`
D) `setResponseCookie()`

Answer: A) `addCookie()` A) All session attributes as a string


B) A single session attribute value
C) Enumeration of attribute names
D) The number of session attributes

Answer: B) A single session attribute value A) `getName()`


B) `retrieveName()`
C) `getCookieName()`
D) `fetchName()`

Answer: A) `getName()`

37. The `HttpServletResponse` method `setContentLength()` is used to:


A) Set the content type of the response
B) Define the servlet's content type
C) Set the character encoding of the response
D) Set the length of the content in the response

Answer: D) Set the length of the content in the response

38. Which method in `HttpSession` is used to set an attribute in the session?


A) `setAttribute()`
B) `setSessionAttribute()`
C) `addAttribute()`
D) `addSessionAttribute()`

Answer: A) `setAttribute()`

pg. 28
UNIT 6

39. The `HttpServletRequest` method `getServletPath()` returns:


A) The server's path
B) The servlet's path
C) The context path
D) The query string

Answer: B) The servlet's path A) `setContentType()`


B) `setCharacterEncoding()`
C) `setContentEncoding()`
D) `setContentTypeAndEncoding()`

Answer: C) `setContentEncoding()`

1. What type of request is typically used for submitting form data and sending large amounts of
data to the server?
A) HTTP GET
B) HTTP POST
C) HTTP PUT
D) HTTP DELETE

Answer: B) HTTP POST

2. Which HTTP method appends data to the URL and is visible in the browser's address bar?
A) POST
B) PUT
C) GET
D) DELETE

Answer: C) GET A) 2 KB
B) 4 KB
C) 8 KB

pg. 29
UNIT 6

D) There's no defined limit


Answer: D) There's no defined limit
4. Which HTTP request method is considered non-idempotent?
A) GET
B) POST
C) PUT
D) DELETE
Answer: B) POST
5. In a URL, where does the data in an HTTP GET request reside?
A) Request body
B) Query string
C) Request header
D) Response body

Answer: B) Query string

6. Which HTTP request method is often used for retrieving data from the server?
A) POST
B) PUT
C) GET
D) PATCH

Answer: C) GET

7. Which request method is less secure as it exposes sensitive information in the URL?
A) POST
B) GET
C) DELETE
D) PUT

Answer: B) GET

pg. 30
UNIT 6

8. What does a query parameter look like in an HTTP GET request?


A) key=value
B) /key/value
C) ?key=value
D) &key=value

Answer: C) ?key=value
9. Which HTTP method is suitable for creating new resources on the server?
A) GET
B) POST
C) PUT
D) DELETE

Answer: B) POST

10. In an HTTP POST request, where does the data typically reside?
A) URL parameters
B) Request body
C) Request header
D) Query string

Answer: B) Request body

11. Which request method should be used for updating an existing resource on the server?
A) PUT
B) GET
C) POST
D) DELETE

Answer: A) PUT

12. Which HTTP method should be used to delete a resource on the server?

pg. 31
UNIT 6

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

Answer: C) DELETE
13. What HTTP response status code is typically returned for a successful GET request?
A) 200 OK
B) 201 Created
C) 400 Bad Request
D) 404 Not Found

Answer: A) 200 OK

14. Which method should handle an HTTP GET request in a servlet?


A) `doGet()`
B) `doPost()`
C) `service()`
D) `doRequest()`

Answer: A) `doGet()`

15. Which parameter in a servlet's `doGet()` method retrieves the request information?
A) `HttpServletResponse`
B) `HttpRequest`
C) `HttpServletRequest`
D) `ServletResponse`

Answer: C) `HttpServletRequest`

16. In an HTTP POST request, where is the content type defined?


A) In the request URL

pg. 32
UNIT 6

B) In the query string


C) In the request body
D) In the request header

Answer: D) In the request header


17. What does the HTTP response status code 404 indicate?
A) Internal Server Error
B) Not Found
C) Forbidden
D) OK

Answer: B) Not Found

18. Which method in a servlet handles an HTTP POST request?


A) `doGet()`
B) `doPost()`
C) `service()`
D) `handleRequest()`

Answer: B) `doPost()`

19. In an HTTP POST request, where is the data sent from the client typically stored?
A) Request body
B) Query parameters
C) Request header
D) Servlet context
Answer: A) Request body

20. What is the primary difference between HTTP GET and POST requests?
A) GET requests are more secure than POST requests
B) GET requests are used for updating data, while POST requests are used for retrieval
C) GET requests append data to the URL, while POST requests send it in the request body

pg. 33
UNIT 6

D) GET requests have a limited data size, while POST requests have no such limit

Answer: C) GET requests append data to the URL, while POST requests send it in the
request body
1. What is the primary purpose of using cookies in web development?
A) Storing data on the client-side
B) Storing data on the server-side
C) Handling server requests
D) Managing session attributes

Answer: A) Storing data on the client-side

2. Which HTTP header is used to send a cookie from the server to the client?
A) Set-Cookie
B) Cookie-Send
C) Cookie
D) Set-Session

Answer: A) Set-Cookie

3. What does JSP stand for?


A) JavaScript Pages
B) JavaServer Pages
C) JavaScript Programming
D) Java Session Programming

Answer: B) JavaServer Pages

4. Which directive in JSP is used to include content from another file during the translation
phase?
A) `<jsp:include>`
B) `<%@ include %>`
C) `<% include %>`

pg. 34
UNIT 6

D) `<jsp:directive.include>`

Answer: B) `<%@ include %>`


5. What is the primary difference between cookies and sessions in web development?
A) Cookies store data on the server, while sessions store data on the client-side.
B) Cookies store data on the client-side, while sessions store data on the server.
C) Cookies are used for session management, while sessions are used for data storage.
D) There is no difference; cookies and sessions perform the same function.

Answer: B) Cookies store data on the client-side, while sessions store data on the server.

6. Which JSP implicit object is used for session tracking?


A) `session`
B) `cookie`
C) `request`
D) `response`

Answer: A) `session`

7. What attribute in a JSP page is used to set the session timeout?


A) `session-timeout`
B) `timeout`
C) `session`
D) `timeout-session`

Answer: A) `session-timeout`

8. In JSP, which tag is used to retrieve session attributes?


A) `<session:get>`
B) `<jsp:session>`
C) `<%@ session %>`
D) `<%= session.getAttribute("attributeName") %>`

pg. 35
UNIT 6

Answer: D) `<%= session.getAttribute("attributeName") %>`


9. What is the default method for session tracking in JSP?
A) Hidden fields
B) Cookies
C) URL Rewriting
D) Session objects

Answer: B) Cookies

10. Which JSP action is used to remove a session attribute?


A) `<jsp:remove>`
B) `<session:remove>`
C) `<%= session.removeAttribute("attributeName") %>`
D) `<% session.remove("attributeName"); %>`

Answer: C) `<%= session.removeAttribute("attributeName") %>`

11. What does the `<%@ page session="false" %>` directive do in a JSP page?
A) Enables session tracking for the page
B) Disables session tracking for the page
C) Sets the session timeout to zero
D) Sets a specific session ID for the page

Answer: B) Disables session tracking for the page

12. Which JSP implicit object is used to retrieve information about the client's request?
A) `request`
B) `session`
C) `application`
D) `response`

pg. 36
UNIT 6

Answer: A) `request`
13. In JSP, how can you set a cookie?
A) Using `<cookie:set>`
B) Using `<jsp:cookie>`
C) Using `<%= response.setCookie("cookieName", "value") %>`
D) Using Java code within scriptlets

Answer: D) Using Java code within scriptlets

14. What is the purpose of URL rewriting in session tracking?


A) Encrypting session data in the URL
B) Adding session information to the URL
C) Hiding session data from the client
D) Removing session data from the URL

Answer: B) Adding session information to the URL

15. How can you access session attributes in JSP?


A) Using the `<session:get>`
B) Using Java code within scriptlets
C) Using `<jsp:session>`
D) Using the `<%= session.getAttribute("attributeName") %>` expression

Answer: D) Using the `<%= session.getAttribute("attributeName") %>` expression

16. Which directive is used to declare a session attribute in a JSP page?


A) `<jsp:attribute>`
B) `<%@ attribute %>`
C) `<%@ page %>`
D) `<jsp:useBean>`

Answer: D) `<jsp:useBean>`

pg. 37
UNIT 6

17. What information can be stored in a session object?


A) Temporary session IDs
B) User-specific data
C) Server configuration details
D) JavaScript code

Answer: B) User-specific data

18. How can you invalidate a session in JSP?


A) `<session:invalidate>`
B) `<%= session.invalidate() %>`
C) Using Java code within scriptlets
D) `<jsp:session invalidate="true">`

Answer: C) Using Java code within scriptlets

19. Which JSP tag is used to forward the control to another resource?
A) `<jsp:forward>`
B) `<forward:page>`
C) `<%@ forward %>`
D) `<jsp:redirect>`

Answer: A) `<jsp:forward>`

20. What is the main advantage of using session tracking in web applications?
A) Enhanced security
B) Improved performance
C) Ability to maintain stateful communication
D) Simpler implementation process

Answer: C) Ability to maintain stateful communication

pg. 38

You might also like