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

Q 1 - Which of the following is the correct order of servlet life cycle phase methods?

A - init(), service(), destroy()

B - initialize(), service(), destroy()

C - init(), execute(), destroy()

D - init(), service(), delete()

Answer : A

Explaination

The servlet is initialized by calling the init () method. The servlet calls service() method to process a client's request. The servlet is terminated by calling the destroy() method.

Q 2 - Which of the following method can be used to get the multiple values of a parameter like checkbox data?

A - request.getParameter()

B - request.getParameterValues()

C - request.getParameterNames()

D - None of the above.

Answer : B

Explaination

You call request.getParameterValues() method if the parameter appears more than once and returns multiple values, for example checkbox.

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 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 checks whether this request was made using a secure channel, such as HTTPS?

A - response.isSecure()

B - request.isSafe()

C - Header.isSecure()

D - None of the above.

Answer : B

Explaination

request.isSecure() returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.

Q 6 - Which of the following code can be used to redirect user to different url?

A - request.sendRedirect(location)

B - response.sendRedirect(location)

C - header.sendRedirect(location)

D - None of the above.

Answer : B

Explaination

response.sendRedirect(location) sends a temporary redirect response to the client using the specified redirect location URL.

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

A - request.setContentLength(length)

B - response.setContentLength(length)

C - header.setContentLength(length)

D - None of the above.

Answer : B

Explaination

response.setContentLength(length) sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header.

Q 8 - Which of the following request attributes that an error-handling servlet can access to analyse the nature of error/exception?

A - javax.servlet.error.status_code

B - javax.servlet.error.exception_type

C - javax.servlet.error.message

D - All of the above.

Answer : D

Explaination

All of the above request attributes can be accessed by error-handling servlet.

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