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.

Answer : A

Explaination

javax.servlet.Servlet is an interface.

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

A - response.getCookies()

B - request.getCookies()

C - Cookies.getCookies()

D - None of the above.

Answer : B

Explaination

request.getCookies() returns an array containing all of the Cookie objects the client sent with this request.

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 returns the port number on which this request was received?

A - response.getServerPort()

B - request.getServerPort()

C - Header.getServerPort()

D - None of the above.

Answer : B

Explaination

request.getServerPort() returns the port number on which this request was received.

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 content type for the body of the response?

A - request.setContentType(type)

B - response.setContentType(type)

C - header.setContentType(type)

D - None of the above.

Answer : B

Explaination

response.setContentType(type) sets the content type for the body of the response.

Q 9 - Which of the following code is used to get session id of a HTTP Session object in servlets?

A - session.getSessionId()

B - session.getId()

C - session.getActiveId()

D - None of the above.

Answer : B

Explaination

session.getId() returns a string containing the unique identifier assigned to this session.

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

A - request.getDisplayCountry()

B - Locale.getDisplayCountry()

C - response.getDisplayCountry()

D - None of the above.

Answer : A

Explaination

request.getDisplayCountry() returns the country/region name for the locale's country that is appropriate for display to the user.

servlets-questions-answers.htm
Advertisements