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 - What is javax.servlet.http.HttpServlet?

A - interface

B - abstract class

C - concreate class

D - None of the above.

Answer : B

Explaination

javax.servlet.http.HttpServlet is an abstract class.

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

Answer : B

Explaination

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

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 8 - Which element of web.xml is used to specify the error handler in servlets?

A - error-page

B - error-handler

C - exception

D - exception-handler

Answer : A

Explaination

You would have to use the error-page element in web.xml to specify the invocation of servlets in response to certain exceptions or HTTP status codes.

Answer : B

Explaination

You can use setMaxAge to delete a cookie by setting is zero.

Q 10 - Which of the following code is used to delete an attribute from a HTTP Session object in servlets?

A - session.removeAttribute(name)

B - session.alterAttribute(name)

C - session.updateAttribute(name)

D - None of the above.

Answer : A

Explaination

session.removeAttribute() removes the object bound with the specified name from this session.

servlets-questions-answers.htm
Advertisements