Web Design and Development MCQs with Answers
Practice important Web Design and Development MCQs with answers and explanations.
Chapter: 30
Q461:
What should be used to store data that needs to be shared across multiple Servlets?
A. ServletConfig
B. ServletContext
C. HttpSession
D. ServletRequest
Correct Answer:
B
Chapter: 30
Q462:
What does the response.sendRedirect() method do in servlets?
A. Forwards the request to another resource
B. Redirects the response to another URL
C. Includes another resource in the response
D. Sends error response to the client
Correct Answer:
B
Chapter: 30
Q463:
What is the main difference between response.sendRedirect() and RequestDispatcher.forward()?
A. sendRedirect() creates a new request, forward() does not
B. sendRedirect() includes another resource, forward() does not
C. sendRedirect() does not keep request attributes, forward() does
D. sendRedirect() is used for error handling, forward() is not
Correct Answer:
C
Chapter: 30
Q464:
What does request.getAttribute() method return?
A. The value of the HTTP request header
B. The value of an attribute set by setAttribute()
C. The URL of the request
D. The HTTP method used for the request
Correct Answer:
B
Chapter: 30
Q465:
In which method of HttpServletRequest can you get the HTTP method used for the request?
A. getAttribute()
B. getMethod()
C. getRequestURL()
D. getProtocol()
Correct Answer:
B
Chapter: 30
Q466:
Which method is used to include the output of another resource in the response?
A. sendRedirect()
B. forward()
C. include()
D. setContentType()
Correct Answer:
C
Chapter: 30
Q467:
What is the correct way to forward a request to another servlet?
A. request.forward("/servlet")
B. response.sendRedirect("/servlet")
C. request.getRequestDispatcher("/servlet").forward(request, response)
D. request.include("/servlet")
Correct Answer:
C
Chapter: 30
Q468:
How does sendRedirect() handle request and response objects?
A. It keeps the original request object
B. It creates a new request object
C. It includes the original request object
D. It forwards the original request object
Correct Answer:
B
Chapter: 30
Q469:
Which method of HttpServletResponse is used to set the content type of the response?
A. setContentLength()
B. setContentType()
C. addCookie()
D. sendRedirect()
Correct Answer:
B
Chapter: 30
Q470:
What does the getRequestURL() method of HttpServletRequest return?
A. The full URL of the request including query string
B. The part of the request URL without the query string
C. The protocol used for the request
D. The HTTP method used for the request
Correct Answer:
B
Chapter: 30
Q471:
What is the main purpose of session tracking in web applications?
A. To handle HTTP protocol errors
B. To maintain state across multiple requests
C. To manage content type of responses
D. To forward requests between servlets
Correct Answer:
B
Chapter: 30
Q472:
In which scenario would you use response.sendError() method?
A. To include another resource's output
B. To redirect to another URL
C. To handle HTTP errors and redirect to an error page
D. To set request attributes
Correct Answer:
C
Chapter: 31
Q473:
What does request.setAttribute() method do?
A. Sets an attribute for the response object
B. Sets an attribute for the request object
C. Sets the content type of the response
D. Sets the request URL
Correct Answer:
B
Chapter: 31
Q474:
How many servlets are defined in the given web.xml configuration file?
A. 1
B. 2
C. 3
D. 4
Correct Answer:
B
Chapter: 31
Q475:
What should be avoided in a Forward request dispatching scenario regarding PrintWriter?
A. Using it in the calling servlet
B. Using it in the included servlet
C. Closing it in the included servlet
D. Using it in both calling and included servlets
Correct Answer:
C
Chapter: 31
Q476:
What is the primary purpose of cookies in web applications?
A. Storing large files
B. Tracking user sessions
C. Improving server performance
D. Encrypting data
Correct Answer:
B
Chapter: 31
Q477:
Which method is used to create a new cookie in Java servlet?
A. Cookie.setCookie()
B. Cookie.create()
C. Cookie()
D. Cookie.add()
Correct Answer:
C
Chapter: 31
Q478:
How does the server identify if a user has visited before using cookies?
A. By IP address
B. By checking for a specific cookie name
C. By analyzing the browser's user agent
D. By the session ID header
Correct Answer:
B
Chapter: 31
Q479:
What does the setMaxAge(int seconds) method do for a cookie in Java?
A. Sets the cookie's maximum size in bytes
B. Sets the time duration before the cookie expires
C. Sets the priority of the cookie
D. Sets the cookie's path for sharing
Correct Answer:
B
Chapter: 31
Q480:
What is the default behavior of cookies if setMaxAge() is not called?
A. The cookie is persistent until explicitly deleted
B. The cookie is session-based and expires when the browser is closed
C. The cookie is stored permanently on the disk
D. The cookie is only valid for a specific domain
Correct Answer:
B