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

Explaination

The servlet is terminated by calling the destroy() method. After the destroy() method is called, the servlet object is marked for garbage collection.

Q 3 - Which of the following code retrieves the body of the request as binary data?

A - new InputStream()

B - response.getInputStream()

C - request.getInputStream()

D - None of the above.

Answer : C

Explaination

request.getInputStream() retrieves the body of the request as binary data using a ServletInputStream.

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 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 send an error response to the client using the specified status code and clearing the buffer.

A - request.sendError(statusCode)

B - response.sendError(statusCode)

C - header.sendError(statusCode)

D - None of the above.

Answer : B

Explaination

response.sendError(statusCode) sends an error response to the client using the specified status code and clearing the buffer.

Q 7 - Which of the following code can be used to set the character encoding for the body of the response?

A - response.setCharacterEncoding(charset)

B - request.setCharacterEncoding(charset)

C - header.setCharacterEncoding(charset)

D - None of the above.

Answer : A

Explaination

response.setCharacterEncoding(charset) sets the character encodinge for the body of the response.

Answer : A

Explaination

Create cookie using new operator on Cookie class.

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