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.
Q 1 - When init() method of servlet gets called?
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.
Q 2 - What is javax.servlet.Servlet?
javax.servlet.Servlet is an interface.
Q 3 - Which of the following code is used to get cookies in servlet?
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?
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?
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?
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?
response.setCharacterEncoding(charset) sets the character encodinge for the body of the response.
Q 8 - How to create a cookie in servlet?
Create cookie using new operator on Cookie class.
Q 9 - Which of the following code delete a cookie in servlet?
You can use setMaxAge to delete a cookie by setting is zero.
Q 10 - Which of the following code is used to set the session timeout in servlets?
You can call public void setMaxInactiveInterval(int interval) method of session to set the timeout for a session individually.