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

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 code is used to set content type of a page to be serviced using servlet?

A - response.setContentType()

B - request.setContentType()

C - writer.setContentType()

D - None of the above.

Answer : A

Explaination

You call response.setContentType() method to set content type of a page to be serviced using servlet.

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 indicates whether the response has been committed?

A - response.isCommitted()

B - request.isCommitted()

C - Header.isCommitted()

D - None of the above.

Answer : A

Explaination

response.isCommitted() returns a boolean indicating if the response has been committed.

Q 6 - Which of the following code can be used to force any content in the buffer to be written to the client?

A - request.flushBuffer()

B - response.flush()

C - response.flushBuffer()

D - None of the above.

Answer : C

Explaination

response.flushBuffer() forces any content in the buffer to be written to the client.

Q 7 - Which of the following code can be used to set the status code of the response?

A - response.setStatus(statusCode)

B - request..setStatus(statusCode)

C - header..setStatus(statusCode)

D - None of the above.

Answer : A

Explaination

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

Answer : A

Explaination

response.addCookie(cookie) adds cookies in the HTTP response header.

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

A - response.getDisplayLanguage()

B - Locale.getDisplayLanguage()

C - request.getDisplayLanguage()

D - None of the above.

Answer : C

Explaination

request.getDisplayLanguage() returns a name for the locale's language that is appropriate for display to the user.

servlets-questions-answers.htm
Advertisements