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.
Q 1 - Which of the following is true about init() method of servlet?
B - The init() method is not called again and again for each user request.
Answer : C
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. It simply creates or loads some data that will be used throughout the life of the servlet.
Q 2 - Which of the following is true about HTTP Post method?
A - The POST method sends the encoded user information as a seperate message to page request.
Answer : C
Explaination
The POST method sends the encoded user information as a seperate message to page request. It is used to submit form data normally.
Q 3 - Which of the following code retrieves the body of the request as binary data?
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 any extra path information associated with the URL the client sent?
Answer : C
Explaination
request.getPathInfo() returns any extra path information associated with the URL the client sent when it made this request.
Q 5 - Which of the following code retrieves the login of the user making this request?
Answer : A
Explaination
request.getRemoteUser() returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.
Q 6 - Which of the following code can be used to add a date header?
A - response.addDateHeader(name,date)
B - request.addDateHeader(name,date)
Answer : A
Explaination
response.addDateHeader(name,date) adds a response header with the given name and date-value.
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)
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 - How to create a cookie in servlet?
B - Use request.getCookie() method
Answer : A
Explaination
Create cookie using new operator on Cookie class.
Q 9 - Which of the following code is used to delete a HTTP Session object in servlets?
Answer : A
Explaination
session.invalidate() invalidates this session and unbinds any objects bound to it.
Q 10 - Which of the following code is used to get language code in servlets?
Answer : C
Explaination
request.getLanguage() returns the language code in lower case for this locale in ISO 639 format.