
- Servlets - Home
- Servlets - Overview
- Servlets - Environment Setup
- Servlets - Life Cycle
- Servlets - Examples
- Servlets - Form Data
- Servlets - Client Request
- Servlets - Server Response
- Servlets - Http Codes
- Servlets - Writing Filters
- Servlets - Exceptions
- Servlets - Cookies Handling
- Servlets - Session Tracking
- Servlets - Database Access
- Servlets - File Uploading
- Servlets - Handling Date
- Servlets - Page Redirect
- Servlets - Hits Counter
- Servlets - Auto Refresh
- Servlets - Sending Email
- Servlets - Packaging
- Servlets - Debugging
- Servlets - Internationalization
- Servlets - Annotations
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 - When init() method of servlet gets called?
A - The init() method is called when the servlet is first created.
B - The init() method is called whenever the servlet is invoked.
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.
Q 2 - What is javax.servlet.http.HttpServlet?
Answer : B
Explaination
javax.servlet.http.HttpServlet is an abstract class.
Q 3 - Which of the following code is used to get names of the attributes in servlet?
A - response.getAttributeNames()
B - request.getAttributeNames()
Answer : B
Explaination
request.getAttributeNames() returns an enumeration containing the names of the attributes available to this request.
Q 4 - Which of the following code retrieves name and version of the protocol?
Answer : C
Explaination
request.getProtocol() returns the name and version of the protocol the request.
Q 5 - Which of the following code indicates whether the named response header has already been set?
A - response.containsHeader(headerName)
B - request.containsHeader(headerName)
Answer : A
Explaination
response.containsHeader(headerName) returns a boolean indicating whether the named response header has already been set.
Q 6 - Which of the following code can be used to add a header?
A - request.addHeader(name,value)
B - response.addDateHeader(name,value)
Answer : B
Explaination
response.addHeader(name,date) adds a response header with the given name and value.
Q 7 - Which of the following code can be used to set the locale of the response?
Answer : B
Explaination
response.setLocale(locale) sets the locale of the response.
Q 8 - Which element of web.xml is used to specify the error handler in servlets?
Answer : A
Explaination
You would have to use the error-page element in web.xml to specify the invocation of servlets in response to certain exceptions or HTTP status codes.
Q 9 - Which of the following code delete a cookie in servlet?
A - response.deleteCookie(cookie);
Answer : B
Explaination
You can use setMaxAge to delete a cookie by setting is zero.
Q 10 - Which of the following code is used to delete an attribute from a HTTP Session object in servlets?
A - session.removeAttribute(name)
B - session.alterAttribute(name)
Answer : A
Explaination
session.removeAttribute() removes the object bound with the specified name from this session.