31.) What is a Servlet context listener, and what is its primary use?
A) It’s a filter that intercepts HTTP requests.
B) It’s a Servlet that handles database connections.
C) It’s an event listener for Servlet context initialization and destruction.
D) It’s a component for managing session data.
32.) In a Java Servlet, how can you access request parameters when handling a POST request?
A) Using the getParameter() method of HttpServletRequest.
B) Using the request.params object.
C) Accessing POST_PARAMETERS in ServletContext.
D) POST request parameters cannot be accessed in a Servlet.
33.) Which HTTP method should you use for an operation that has no side effects and is idempotent?
A) GET
B) POST
C) PUT
D) DELETE
34.) What is Cross-Site Request Forgery (CSRF), and how can it be prevented in a Java Servlet?
A) CSRF is an attack that injects malicious scripts into web pages.
B) CSRF is an attack that tricks a user into performing an unintended action.
C) To prevent CSRF, use token-based authentication and validate tokens in Servlets.
D) CSRF can be prevented by using strong encryption for session data.
35.) When deploying a Java web application, where should you place the web resources like HTML, CSS, and JavaScript files?
A) In the WEB-INF directory.
B) In the root directory of the web application.
C) In the META-INF directory.
D) In a separate external directory.
36.) What is the purpose of the web.xml file’s element in a Java web application?
A) A. To define Servlet mappings.
B) To configure session management.
C) To specify initialization parameters for the Servlet context.
D) To define error pages.
37.) In a Java Servlet, what is the purpose of an error page defined in the web.xml file?
A) To handle all errors that occur in the Servlet.
B) To specify the location of custom error pages for specific HTTP status codes.
C) To define error messages for users.
D) To handle database errors.
38.) When an unhandled exception occurs in a Java Servlet, what HTTP status code is typically sent in the response?
A) 200 OK
B) 404 Not Found
C) 500 Internal Server Error
D) 302 Found (Redirect)
39.) What is the purpose of the “Same Origin Policy” in web security?
A) To prevent Cross-Site Scripting (XSS) attacks.
B) To restrict web pages from making requests to different domains.
C) To encrypt session data during transmission.
D) To authenticate users.
40.) How can you implement user authentication in a Java Servlet-based web application?
A) By using the request.authenticate() method.
B) By checking the request.isAuthorized() property.
C) By configuring authentication in the web.xml file.
D) By handling authentication within the Servlet code.