Servlets - Online Quiz



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

Q 1 - Which of the following is the correct order of servlet life cycle phase methods?

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

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

C - init(), execute(), destroy()

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

Answer : A

Explaination

The servlet is initialized by calling the init () method. The servlet calls service() method to process a client's request. The servlet is terminated by calling the destroy() method.

Q 2 - Which of the following method can be used to get the value of form parameter?

A - request.getParameter()

B - request.getParameterValues()

C - request.getParameterNames()

D - None of the above.

Answer : A

Explaination

You call request.getParameter() method to get the value of a form parameter.

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

A - response.getCookies()

B - request.getCookies()

C - Cookies.getCookies()

D - None of the above.

Answer : B

Explaination

request.getCookies() returns an array containing all of the Cookie objects the client sent with this request.

Q 4 - Which of the following code retrieves the query string that is contained in the request URL after the path?

A - Header.getQueryString()

B - response.getQueryString()

C - request.getQueryString()

D - None of the above.

Answer : C

Explaination

request.getQueryString() returns the query string that is contained in the request URL after the path.

Q 5 - Which of the following code retrieves session ID specified by the client?

A - request.getRequestedSessionId()

B - response.getRequestedSessionId()

C - Header.getRequestedSessionId()

D - None of the above.

Answer : A

Explaination

request.getRequestedSessionId() returns the session ID specified by the client.

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 code can be used to set the locale of the response?

A - request.setLocale(locale)

B - response.setLocale(locale)

C - header.setLocale(locale)

D - None of the above.

Answer : B

Explaination

response.setLocale(locale) sets the locale of the response.

Answer : A

Explaination

The destroy() method is called only once at the end of the life cycle of a filter.

Q 9 - Which of the following way can be used to keep track of previous client request?

A - Using cookies.

B - Using hidden form fields.

C - Using URL rewriting.

D - All of the above.

Answer : D

Explaination

All of the above can be used to keep track of previous client request.

Q 10 - Which of the following code is used to set auto refresh of a page after 5 seconds?

A - session.setIntHeader("Refresh", 5)

B - response.setIntHeader("Refresh", 5)

C - request.setIntHeader("Refresh", 5)

D - None of the above.

Answer : B

Explaination

response.setIntHeader("Refresh", 5) sets refresh, autoload time as 5 seconds.

servlets-questions-answers.htm
Advertisements