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 : A

Explaination

The init method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request. So, it is used for one-time initializations, just as with the init method of applets.

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 PrintWriter object in servlet?

A - response.getWriter()

B - request.getWriter()

C - new PrintWriter()

D - None of the above.

Answer : A

Explaination

You call response.getWriter() method to get PrintWriter object in servlet.

Q 4 - Which of the following code retrieves the context of the request?

A - new ClassContextPath()

B - request.getContextPath()

C - response.getContextPath()

D - None of the above.

Answer : B

Explaination

request.getContextPath() returns the portion of the request URI that indicates the context of the request.

Q 5 - Which of the following code retrieves the fully qualified name of the client making this request?

A - request.getRemoteHost()

B - response.getRemoteHost()

C - Header.getRemoteHost()

D - None of the above.

Answer : A

Explaination

request.getRemoteHost() returns the fully qualified name of the client that sent the request.

Q 6 - Which of the following code can be used to redirect user to different url?

A - request.sendRedirect(location)

B - response.sendRedirect(location)

C - header.sendRedirect(location)

D - None of the above.

Answer : B

Explaination

response.sendRedirect(location) sends a temporary redirect response to the client using the specified redirect location URL.

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 of the following request attributes that an error-handling servlet can access to analyse the nature of error/exception?

A - javax.servlet.error.status_code

B - javax.servlet.error.exception_type

C - javax.servlet.error.message

D - All of the above.

Answer : D

Explaination

All of the above request attributes can be accessed by error-handling servlet.

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