Servlets - Online Quiz


Advertisements


Following quiz provides Multiple Choice Questions (MCQs) related to Servlets Framework. You will have to read all the given answers and click over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer button. You can use Next Quiz button to check new set of questions in the quiz.

Questions and Answers

Answer : C

Explaination

Java Servlets are programs that run on a Web or Application server and act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server.

Answer : A

Explaination

javax.servlet.Servlet is an interface.

Q 3 - Which of the following code retrieves the body of the request as binary data?

A - new InputStream()

B - response.getInputStream()

C - request.getInputStream()

D - None of the above.

Answer : C

Explaination

request.getInputStream() retrieves the body of the request as binary data using a ServletInputStream.

Q 4 - Which of the following code retrieves name and version of the protocol?

A - Header.getProtocol()

B - response.getProtocol()

C - request.getProtocol()

D - None of the above.

Answer : C

Explaination

request.getProtocol() returns the name and version of the protocol the request.

Q 5 - Which of the following code encodes the specified URL for use in the sendRedirect method?

A - response.encodeRedirectURL(url)

B - request.encodeRedirectURL(url)

C - Header.encodeRedirectURL(url)

D - None of the above.

Answer : A

Explaination

response.encodeRedirectURL(url) encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged.

Q 6 - Which of the following code can be used to send an error response to the client using the specified status code and error message?

A - request.sendError(statusCode,message)

B - response.sendError(statusCode,message)

C - header.sendError(statusCode,message)

D - None of the above.

Answer : B

Explaination

response.sendError(statusCode,message) sends an error response to the client using the specified status code and error message.

Q 7 - Which of the following is the correct order of filter life cycle phase methods?

A - init(), service(), destroy()

B - initialize(), service(), destroy()

C - init(), doFilter(), destroy()

D - init(), service(), delete()

Answer : A

Explaination

init() method is called by the web container to indicate to a filter that it is being placed into service. doFilter() method is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. destroy() method is called by the web container to indicate to a filter that it is being taken out of service.

Q 8 - Which element of web.xml is used to specify the error handler in servlets?

A - error-page

B - error-handler

C - exception

D - exception-handler

Answer : A

Explaination

You would have to use the error-page element in web.xml to specify the invocation of servlets in response to certain exceptions or HTTP status codes.

Q 9 - Which of the following code is used to delete a HTTP Session object in servlets?

A - session.invalidate()

B - response.deleteSession()

C - request.deleteSession()

D - None of the above.

Answer : A

Explaination

session.invalidate() invalidates this session and unbinds any objects bound to it.

Q 10 - Which of the following code is used to get country/region name in servlets?

A - request.getDisplayCountry()

B - Locale.getDisplayCountry()

C - response.getDisplayCountry()

D - None of the above.

Answer : A

Explaination

request.getDisplayCountry() returns the country/region name for the locale's country that is appropriate for display to the user.


servlets-questions-answers.htm

Advertisements