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 : 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 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 by including the session ID in it?

A - response.encodeURL(url)

B - request.encodeURL(url)

C - Header.encodeURL(url)

D - None of the above.

Answer : A

Explaination

response.encodeURL(url) encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged.

Answer : B

Explaination

response.addCookie(cookie) adds the specified cookie to the response and can be used to write a cookie.

Q 7 - Which of the following code can be used to set the status code of the response?

A - response.setStatus(statusCode)

B - request..setStatus(statusCode)

C - header..setStatus(statusCode)

D - None of the above.

Answer : A

Explaination

response.setLocale(locale) sets the status code of the response.

Answer : A

Explaination

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

Q 9 - Does server automatically keep any record of previous client request?

A - true

B - false

Answer : B

Explaination

HTTP is a stateless protocol which means each time a client retrieves a Web page, the client opens a separate connection to the Web server and the server automatically does not keep any record of previous client request.

Answer : A

Explaination

You can call public void setMaxInactiveInterval(int interval) method of session to set the timeout for a session individually.

servlets-questions-answers.htm
Advertisements