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

Explaination

The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client. Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.

Answer : A

Explaination

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

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

A - response.getAttributeNames()

B - request.getAttributeNames()

C - Header.getAttributeNames()

D - None of the above.

Answer : B

Explaination

request.getAttributeNames() returns an enumeration containing the names of the attributes available to this request.

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 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 clear any data that exists in the buffer as well as the status code and headers?

A - request.reset()

B - response.reset()

C - response.resetBuffer()

D - None of the above.

Answer : B

Explaination

response.reset() clears any data that exists in the buffer as well as the status code and headers.

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.

Q 9 - Which of the following code is used to update an attribute in a HTTP Session object in servlets?

A - session.setAttribute(name,value)

B - session.alterAttribute(name,value)

C - session.updateAttribute(name,value)

D - None of the above.

Answer : A

Explaination

session.setAttribute() binds an object to this session, using the name specified.

Q 10 - Which of the following code is used to get country/region code in servlets?

A - request.getCountry()

B - Locale.getCountry()

C - response.getCountry()

D - None of the above.

Answer : A

Explaination

request.getCountry() returns the country/region code in upper case for this locale in ISO 3166 2-letter format.

servlets-questions-answers.htm
Advertisements