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.

Q 2 - Which of the following method can be used to get the multiple values of a parameter like checkbox data?

A - request.getParameter()

B - request.getParameterValues()

C - request.getParameterNames()

D - None of the above.

Answer : B

Explaination

You call request.getParameterValues() method if the parameter appears more than once and returns multiple values, for example checkbox.

Q 3 - Which of the following code is used to get session in servlet?

A - request.getSession()

B - response.getSession()

C - new Session()

D - None of the above.

Answer : A

Explaination

request.getSession() returns the current session associated with this request, or if the request does not have a session, creates one.

Answer : C

Explaination

request.getHeader(headerName) returns the value of the specified request header as a String.

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.

Answer : B

Explaination

You can use setMaxAge to delete a cookie by setting is zero.

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