- 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 - 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 - 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 a particular attribute in servlet?
A - request.getAttribute(name)
Answer : A
Explaination
request.getAttribute(name) returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
Q 4 - Which of the following code retrieves the request header?
A - Header.getHeaderName(headerName)
B - response.getHeader(headerName)()
Answer : C
Explaination
request.getHeader(headerName) returns the value of the specified request header as a String.
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 send an error response to the client using the specified status code and clearing the buffer.
A - request.sendError(statusCode)
B - response.sendError(statusCode)
Answer : B
Explaination
response.sendError(statusCode) sends an error response to the client using the specified status code and clearing the buffer.
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 of the following request attributes that an error-handling servlet can access to analyse the nature of error/exception?
A - javax.servlet.error.status_code
B - javax.servlet.error.exception_type
Answer : D
Explaination
All of the above request attributes can be accessed by error-handling servlet.
Q 9 - Which of the following code is used to add an attribute in a HTTP Session object in servlets?
A - session.addAttribute(name,value)
B - session.setAttribute(name,value)
Answer : B
Explaination
session.setAttribute() binds an object to this session, using the name specified.
Q 10 - Which of the following code is used to set the session timeout in servlets?
A - session.setMaxInactiveInterval(interval)
B - response.setMaxInactiveInterval(interval)
Answer : A
Explaination
You can call public void setMaxInactiveInterval(int interval) method of session to set the timeout for a session individually.
