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.

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 Internet Protocol (IP) address of the client that sent the request?

A - request.getRemoteAddr()

B - response.getRemoteAddr()

C - Header.getRemoteAddr()

D - None of the above.

Answer : A

Explaination

request.getRemoteAddr() returns the Internet Protocol (IP) address of the client that sent the request.

Q 5 - Which of the following code retrieves the fully qualified name of the client making this request?

A - request.getRemoteHost()

B - response.getRemoteHost()

C - Header.getRemoteHost()

D - None of the above.

Answer : A

Explaination

request.getRemoteHost() returns the fully qualified name of the client that sent the request.

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.

Answer : D

Explaination

All of the above filters are suggested by the servlet specifications.

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.

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

A - response.getDisplayLanguage()

B - Locale.getDisplayLanguage()

C - request.getDisplayLanguage()

D - None of the above.

Answer : C

Explaination

request.getDisplayLanguage() returns a name for the locale's language that is appropriate for display to the user.

servlets-questions-answers.htm
Advertisements