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

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 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 retrieves the name of the authentication scheme?

A - new AuthType()

B - response.getAuthType()

C - request.getAuthType()

D - None of the above.

Answer : C

Explaination

request.getAuthType() returns the name of the authentication scheme used to protect the servlet, for example, BASIC or SSL or null if the JSP was not protected.

Q 4 - Which of the following code retrieves the character encoding used in the body of this request?

A - new CharacterEncoding()

B - response.getCharacterEncoding()

C - request.getCharacterEncoding()

D - None of the above.

Answer : C

Explaination

request.getCharacterEncoding() returns the name of the character encoding used in the body of this request.

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.

Answer : B

Explaination

response.addCookie(cookie) adds the specified cookie to the response and can be used to write a cookie.

Answer : C

Explaination

Servlet Filters are Java classes that can be used to intercept requests from a client before they access a resource at back end and to manipulate responses from server before they are sent back to the client.

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 code is used to get a HTTP Session object in servlets?

A - request.getSession()

B - response.getSession();

C - new Session()

D - None of the above.

Answer : A

Explaination

request.getSession() is used to get a HTTP Session object in servlets.

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