0% found this document useful (0 votes)
24 views24 pages

96(7)

The document provides a comprehensive overview of servlets, including their definitions, lifecycle methods, and key interfaces. It covers various aspects such as handling requests and responses, managing sessions, and configuring servlets through annotations and XML. Additionally, it addresses the advantages of servlets over CGI and discusses best practices for ensuring thread safety and managing errors.

Uploaded by

lochansawant0107
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)
24 views24 pages

96(7)

The document provides a comprehensive overview of servlets, including their definitions, lifecycle methods, and key interfaces. It covers various aspects such as handling requests and responses, managing sessions, and configuring servlets through annotations and XML. Additionally, it addresses the advantages of servlets over CGI and discusses best practices for ensuring thread safety and managing errors.

Uploaded by

lochansawant0107
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/ 24

SERVLETS

1. What is a servlet?
a. A server-side program that handles client requests
b. A client-side program that handles server responses
c. A type of web browser
d. A database connection class
Answer: a

2. Which interface must be implemented by all servlets?


a. ServletRequest
b. ServletResponse
c. Servlet
d. HttpServlet
Answer: c

3. What method is called when a servlet is first loaded into memory?


a. init()
b. start()
c. load()
d. config()
Answer: a

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


a. doPost()
b. doGet()
c. service()
d. handle()
Answer: b
5. 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 client requests
d. To send responses
Answer: b

6. Which class is a direct subclass of the Servlet interface?


a. GenericServlet
b. HttpServlet
c. ServletConfig
d. ServletResponse
Answer: a

7. What is the role of the ServletConfig interface?


a. To configure servlet parameters
b. To handle request parameters
c. To manage session data
d. To send responses to clients
Answer: a

8. Which method is used to read parameters sent in a POST request?


a. getParameter()
b. readParameter()
c. fetchParameter()
d. retrieveParameter()
Answer: a

9. What does the doPost() method do in a servlet?


a. Handles HTTP POST requests
b. Handles HTTP GET requests
c. Initializes the servlet
d. Cleans up resources
Answer: a

10. How do you send a response back to the client in a servlet?


a. Using the write() method
b. Using the print() method
c. Using the sendResponse() method
d. Using the ServletResponse object
Answer: d

11. What is the use of the RequestDispatcher interface?


a. To send requests to another servlet or resource
b. To manage session data
c. To handle HTTP methods
d. To read request parameters
Answer: a

12. Which method of the HttpServlet class is used to handle PUT requests?
a. doPut()
b. doRequest()
c. handlePut()
d. put()
Answer: a

13. What is the purpose of the getServletContext() method?


a. To get the servlet's configuration
b. To get the servlet's initialization parameters
c. To get the context for the entire web application
d. To get the request parameters
Answer: c

14. How can you obtain the current session in a servlet?


a. request.getSession()
b. getCurrentSession()
c. obtainSession()
d. getSession()
Answer: a

15. Which of the following is NOT a scope for servlet attributes?


a. Application scope
b. Request scope
c. Session scope
d. Thread scope
Answer: d

16. What is the default HTTP method for a form submission?


a. POST
b. GET
c. PUT
d. DELETE
Answer: a

17. What is the function of the ServletContext interface?


a. To manage a single servlet instance
b. To provide a way to communicate between servlets
c. To provide application-level parameters and attributes
d. To handle request parameters
Answer: c

18. Which method is used to set the content type of a response?


a. setResponseType()
b. setContentType()
c. setHeader()
d. setType()
Answer: b

19. What is the purpose of the HttpServletRequest interface?


a. To send HTTP responses
b. To encapsulate HTTP request data
c. To configure servlets
d. To manage sessions
Answer: b

20. Which of the following is a valid way to include another servlet's response?
a. response.sendRedirect()
b. RequestDispatcher.include()
c. response.include()
d. RequestDispatcher.forward()
Answer: b

21. What does the response.sendRedirect() method do?


a. Sends an error response
b. Redirects the client to a different URL
c. Forwards the request to another resource
d. Sends a static response
Answer: b
22. How do you read a file uploaded via a servlet?
a. Using ServletInputStream
b. Using BufferedReader
c. Using FileReader
d. Using InputStreamReader
Answer: a

23. Which of the following methods can be used to set response headers?
a. setHeader()
b. setResponseHeader()
c. addHeader()
d. Both a and c
Answer: d

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


a. Handles HTTP PUT requests
b. Handles HTTP GET requests
c. Initializes the servlet
d. Cleans up resources
Answer: a

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


a. Servlets are easier to write
b. Servlets can handle multiple requests concurrently
c. Servlets are written in HTML
d. Servlets are client-side technologies
Answer: b

26. Which of the following is true about servlets?


a. They are platform-independent
b. They are stateful by nature
c. They are loaded into the server for every request
d. They run in a separate process from the web server
Answer: a

27. How can you access initialization parameters in a servlet?


a. Using getInitParameter()
b. Using getParameter()
c. Using getConfigParameter()
d. Using getContextParameter()
Answer: a

28. What is the use of the ServletRequest interface?


a. To handle HTTP responses
b. To encapsulate request data from clients
c. To manage session data
d. To configure servlets
Answer: b

29. Which lifecycle method is guaranteed to be called only once?


a. service()
b. init()
c. destroy()
d. doGet()
Answer: b

30. What is the purpose of the HttpServletResponse interface?


a. To encapsulate HTTP request data
b. To send HTTP responses to clients
c. To manage servlet configurations
d. To handle session data
Answer: b

31. How do you forward a request to another servlet?


a. response.sendRedirect()
b. RequestDispatcher.forward()
c. response.forward()
d. include() method
Answer: b

32. What does the getParameterNames() method return?


a. A list of all parameters
b. An enumeration of all parameter names
c. The first parameter name
d. A string array of parameter names
Answer: b

33. What is the default value for the content type of a response?
a. text/html
b. application/json
c. text/plain
d. application/xml
Answer: a

34. Which of the following methods can be overridden to handle any HTTP request?
a. doGet()
b. doPost()
c. service()
d. handleRequest()
Answer: c
35. What does the request.getSession() method return?
a. A new session every time
b. The current session associated with the request
c. Null if no session exists
d. Both b and c
Answer: d

36. Which object is used to create a PrintWriter for sending character text to the client?
a. ServletOutputStream
b. HttpServletResponse
c. PrintWriter
d. ServletResponse
Answer: b

37. What is the typical use case for the session object in a servlet?
a. To store user preferences
b. To manage database connections
c. To configure servlet parameters
d. To send HTTP requests
Answer: a

38. How is a servlet mapped to a URL in a web application?


a. Using the web.xml file
b. Using annotations only
c. Using the servlet's constructor
d. Using a Java class file
Answer: a

39. Which of the following is NOT a valid HTTP method?


a. GET
b. POST
c. PUT
d. LIST
Answer: d

40. What is the role of the web.xml file in a

servlet-based application?
a. To store session data
b. To configure servlet mappings and initialization parameters
c. To define error pages
d. Both b and c
Answer: d

41. Which of the following methods is called to read the body of an HTTP request?
a. getReader()
b. getInputStream()
c. readRequestBody()
d. Both a and b
Answer: d

42. What is the significance of the servlet's life cycle?


a. It defines how servlets handle requests
b. It manages resource allocation
c. It determines the servlet's performance
d. All of the above
Answer: d

43. What is the main disadvantage of using session attributes?


a. They are lost when the server restarts
b. They consume more memory
c. They are not secure
d. Both a and b
Answer: d

44. How do you make a servlet thread-safe?


a. Synchronizing methods
b. Using the singleton pattern
c. Avoiding shared data
d. All of the above
Answer: d

45. What is the purpose of the @WebServlet annotation?


a. To define a servlet class
b. To configure servlet mapping without web.xml
c. To set servlet initialization parameters
d. Both a and b
Answer: d

46. What happens if a servlet's init() method throws an exception?


a. The servlet is initialized anyway
b. The servlet is marked as unavailable
c. The servlet will be retried
d. The application will crash
Answer: b

47. How can you access servlet context attributes?


a. Using getServletContext()
b. Using getContextAttribute()
c. Using getAttribute()
d. Both a and c
Answer: d

48. What type of data can be sent in the body of an HTTP request?
a. Text only
b. Binary data only
c. Both text and binary data
d. Only JSON data
Answer: c

49. What is the default session timeout in a servlet?


a. 10 minutes
b. 30 minutes
c. 60 minutes
d. 120 minutes
Answer: b

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


a. session.invalidate()
b. session.destroy()
c. session.close()
d. session.remove()
Answer: a

51. Which of the following is a valid way to send binary data in a servlet response?
a. Using PrintWriter
b. Using ServletOutputStream
c. Using BufferedWriter
d. Using Writer
Answer: b

52. What is the purpose of the getServletInfo() method?


a. To provide information about the servlet
b. To retrieve request data
c. To manage session data
d. To handle errors
Answer: a

53. What is the main purpose of the HttpServletResponse interface?


a. To read HTTP requests
b. To configure servlets
c. To send HTTP responses
d. To manage sessions
Answer: c

54. What method do you call to redirect a request to a different URL?


a. sendRedirect()
b. redirect()
c. forward()
d. include()
Answer: a

55. Which method can you use to retrieve cookies from a request?
a. getCookies()
b. retrieveCookies()
c. getAllCookies()
d. fetchCookies()
Answer: a
56. How can you set the character encoding of a request?
a. request.setCharacterEncoding()
b. setCharacterEncoding()
c. setEncoding()
d. configureEncoding()
Answer: a

57. What is the role of the PrintWriter object in a servlet?


a. To read input from the client
b. To write text data to the response
c. To configure servlets
d. To manage sessions
Answer: b

58. Which of the following can be used to manage error pages in a web application?
a. web.xml
b. servlet code
c. JSP files
d. Both a and c
Answer: d

59. How do you obtain a specific attribute from the request?


a. request.getAttribute("attributeName")
b. request.getParameter("attributeName")
c. request.getContext("attributeName")
d. request.getRequestAttribute("attributeName")
Answer: a

60. What does the ServletOutputStream class do?


a. It reads data from the client
b. It writes binary data to the response
c. It writes character data to the response
d. It handles session data
Answer: b

61. Which method should be overridden to handle errors in a servlet?


a. doGet()
b. doPost()
c. service()
d. handleError()
Answer: c

62. What is the significance of the Servlet API version?


a. It determines the servlet's performance
b. It defines the features and capabilities available
c. It affects the way servlets are coded
d. Both b and c
Answer: d

63. How can you set an attribute in a session?


a. session.setAttribute("key", value)
b. session.addAttribute("key", value)
c. session.putAttribute("key", value)
d. session.updateAttribute("key", value)
Answer: a

64. What is the result of calling response.getWriter() in a servlet?


a. A PrintWriter for writing text to the response
b. A BufferedWriter for writing data
c. An InputStream for reading data
d. A ServletOutputStream for writing binary data
Answer: a

65. Which of the following is NOT a valid servlet lifecycle phase?


a. Initialization
b. Request handling
c. Destruction
d. Compilation
Answer: d

66. What is the main purpose of a filter in a servlet-based application?


a. To handle requests directly
b. To modify request and response objects
c. To manage sessions
d. To configure servlets
Answer: b

67. Which of the following is a valid way to access the servlet's initialization parameters?
a. getServletConfig().getInitParameter("paramName")
b. getContext().getInitParameter("paramName")
c. getParameter("paramName")
d. getConfigParameter("paramName")
Answer: a

68. What does the method getServletName() return?


a. The name of the servlet
b. The context path
c. The servlet's class name
d. The initialization parameters
Answer: a
69. What is the typical use of the doOptions() method in a servlet?
a. To handle HTTP OPTIONS requests
b. To initialize the servlet
c. To manage session data
d. To send responses
Answer: a

70. Which attribute is used to specify the timeout for a session in web.xml?
a. session-timeout
b. timeout
c. sessionExpiry
d. sessionDuration
Answer: a

71. How do you handle multiple requests in a servlet?


a. By creating a new servlet instance for each request
b. By using synchronized methods
c. By implementing a multi-threaded design
d. Both b and c
Answer: d

72. What is the purpose of using the @WebFilter annotation?


a. To define a servlet
b. To configure filters without web.xml
c. To set initialization parameters
d. To handle session data
Answer: b

73. Which method is called when a servlet receives an HTTP request?


a. init()
b. service()
c. handleRequest()
d. execute()
Answer: b

74. What type of data can be sent in the query string of a URL?
a. Only text
b. Only binary data
c. Both text and binary data
d. Only JSON data
Answer: a

75. How can you set the character encoding for a response?
a. response.setCharacterEncoding("UTF-8")
b. setEncoding("UTF-8")
c. response.setEncoding("UTF-8")
d. response.setContentType("text/html; charset=UTF-8")
Answer: a

76. Which of the following methods can be overridden to handle HTTP HEAD requests?
a. doGet()
b. doHead()
c. doOptions()
d. doPost()
Answer: b

77. What is the purpose of the sendError() method in a servlet?


a. To send a custom error response
b. To redirect to another page
c. To log errors
d. To invalidate a session
Answer: a

78.

What is the result of calling request.getRequestDispatcher("path").forward(request,


response)?
a. The request is forwarded to another resource
b. A redirect to another URL occurs
c. An error occurs
d. The response is terminated
Answer: a

79. How can you obtain the server's context path in a servlet?
a. request.getContextPath()
b. getContextPath()
c. context.getPath()
d. request.getPath()
Answer: a

80. What is the effect of using a session attribute?


a. Data is shared across multiple requests from the same client
b. Data is lost after the servlet is destroyed
c. Data is accessible only during a single request
d. Both a and b
Answer: a

81. Which method is used to check if a session is new?


a. isNew()
b. session.isNew()
c. isNewSession()
d. checkNew()
Answer: b

82. How do you set a cookie in a servlet response?


a. response.addCookie(cookie)
b. response.setCookie(cookie)
c. response.createCookie(cookie)
d. response.putCookie(cookie)
Answer: a

83. What happens if you call response.getWriter() after response has been committed?
a. It throws an exception
b. It returns a null PrintWriter
c. It works normally
d. It generates an empty response
Answer: a

84. Which HTTP method is idempotent?


a. GET
b. POST
c. DELETE
d. Both a and c
Answer: d

85. How can you set the status code of a response?


a. response.setStatus(code)
b. response.setResponseCode(code)
c. response.statusCode(code)
d. setStatus(code)
Answer: a

86. What does the setContentLength() method do?


a. Sets the length of the response body
b. Configures the maximum content length
c. Specifies the length of the request body
d. Sets the length of the session
Answer: a

87. Which of the following is NOT part of the servlet lifecycle?


a. Initialization
b. Request handling
c. Execution
d. Destruction
Answer: c

88. What is the function of the HttpServlet class?


a. To handle HTTP requests specifically
b. To provide a base class for servlets
c. To manage session data
d. Both a and b
Answer: d

89. How do you retrieve all cookies from a request?


a. request.getCookies()
b. request.getAllCookies()
c. request.retrieveCookies()
d. request.getCookiesArray()
Answer: a
90. Which method would you use to handle an HTTP PATCH request?
a. doPatch()
b. doUpdate()
c. doOptions()
d. doPut()
Answer: a

91. What does the method response.flushBuffer() do?


a. Sends the response immediately
b. Clears the response buffer
c. Discards the response
d. Both a and b
Answer: d

92. How can you determine the client's IP address in a servlet?


a. request.getRemoteAddr()
b. request.getClientIp()
c. request.getIPAddress()
d. request.getRemoteHost()
Answer: a

93. What is the main advantage of using asynchronous servlets?


a. Improved performance for long-running tasks
b. Simpler code
c. Less memory consumption
d. More secure connections
Answer: a

94. Which method is used to handle concurrent requests in servlets?


a. service()
b. doConcurrent()
c. handleRequest()
d. executeConcurrent()
Answer: a

95. What is the purpose of the getPathInfo() method?


a. To retrieve additional path information from the request
b. To get the servlet's path
c. To manage session data
d. To retrieve the request method
Answer: a

96. How do you send a JSON response from a servlet?


a. response.setContentType("application/json")
b. response.write(jsonData)
c. Both a and b
d. None of the above
Answer: c

97. Which of the following is true about servlet filters?


a. They can modify request and response objects
b. They can be used for logging and authentication
c. They are executed before and after a servlet
d. All of the above
Answer: d

98. What is the purpose of the HttpSession interface?


a. To manage user sessions
b. To configure servlets
c. To handle requests
d. To send responses
Answer: a

99. How can you create a new session in a servlet?


a. request.getSession(true)
b. session.create()
c. request.newSession()
d. Both a and c
Answer: a

100. What is the significance of the Servlet 3.0 API?


a. It introduced annotations for servlets and filters
b. It improved performance
c. It made servlets obsolete
d. It provided more error handling
Answer: a

You might also like