Servlets - Online Quiz


Advertisements

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 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 add a header with the given name and integer value?

A - request.addHeader(name,value)

B - response.addIntHeader(name,value)

C - Header.addDateHeader(name,value)

D - None of the above.

Answer : B

Explaination

response.addIntHeader(name,date) adds a response header with the given name and integer value.

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.

Answer : A

Explaination

Create cookie using new operator on Cookie class.

Answer : B

Explaination

You can use setMaxAge to delete a cookie by setting is zero.

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