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 : 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 is used to get locale in servlet?

A - request.getlocale()

B - response.getLocale()

C - new Locale()

D - None of the above.

Answer : A

Explaination

request.getlocale() returns the preferred Locale that the client will accept content in, based on the Accept-Language header.

Q 4 - Which of the following code retrieves name and version of the protocol?

A - Header.getProtocol()

B - response.getProtocol()

C - request.getProtocol()

D - None of the above.

Answer : C

Explaination

request.getProtocol() returns the name and version of the protocol the request.

Q 5 - Which of the following code indicates whether the named response header has already been set?

A - response.containsHeader(headerName)

B - request.containsHeader(headerName)

C - Header.containsHeader(headerName)

D - None of the above.

Answer : A

Explaination

response.containsHeader(headerName) returns a boolean indicating whether the named response header has already been set.

Q 6 - Which of the following code can be used to add a header with the given name and integer value?

A - request.addHeader(name,value)

B - response.addIntHeader(name,value)

C - Header.addDateHeader(name,value)

D - None of the above.

Answer : B

Explaination

response.addIntHeader(name,date) adds a response header with the given name and integer value.

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

A - request.setContentType(type)

B - response.setContentType(type)

C - header.setContentType(type)

D - None of the above.

Answer : B

Explaination

response.setContentType(type) sets the content type for the body of the response.

Answer : B

Explaination

doFilter() method is called by the container each time a request/response pair is passed through the chain due to a client request for a resource/servlet at the end of the chain.

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

A - session.getAttribute(name)

B - session.alterAttribute(name)

C - session.updateAttribute(name)

D - None of the above.

Answer : A

Explaination

session.getAttribute() returns the object bound with the specified name in this session, or null if no object is bound under the name.

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

A - response.getLanguage()

B - Locale.getLanguage()

C - request.getLanguage()

D - None of the above.

Answer : C

Explaination

request.getLanguage() returns the language code in lower case for this locale in ISO 639 format.


servlets-questions-answers.htm

Advertisements