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

Explaination

Each time the server receives a request for a servlet, the server spawns a new thread and calls service() method.

Answer : A

Explaination

javax.servlet.Servlet is an interface.

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

A - request.getSession()

B - response.getSession()

C - new Session()

D - None of the above.

Answer : A

Explaination

request.getSession() returns the current session associated with this request, or if the request does not have a session, creates one.

Q 4 - Which of the following code retrieves any extra path information associated with the URL the client sent?

A - Header.getPathInfo()

B - response.getPathInfo()

C - request.getPathInfo()

D - None of the above.

Answer : C

Explaination

request.getPathInfo() returns any extra path information associated with the URL the client sent when it made 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.

Q 6 - Which of the following code can be used to send an error response to the client using the specified status code and error message?

A - request.sendError(statusCode,message)

B - response.sendError(statusCode,message)

C - header.sendError(statusCode,message)

D - None of the above.

Answer : B

Explaination

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

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.

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.

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 three-letter abbreviation for this locale's country in servlets?

A - request.getISO3Country()

B - Locale.getISO3Country()

C - response.getISO3Country()

D - None of the above.

Answer : A

Explaination

request.getISO3Country() returns a three-letter abbreviation for this locale's country.

servlets-questions-answers.htm
Advertisements