- Servlets Tutorial
- 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 Useful Resources
- Servlets - Questions and Answers
- Servlets - Quick Guide
- Servlets - Useful Resources
- Servlets - Discussion
Servlets - Mock Test
This section presents you various set of Mock Tests related to Servlets Framework. You can download these sample mock tests at your local machine and solve offline at your convenience. Every mock test is supplied with a mock test key to let you verify the final score and grade yourself.
Servlets Mock Test III
Q 1 - Which of the following code can be used to write a cookie?
Answer : B
Explaination
response.addCookie(cookie) adds the specified cookie to the response and can be used to write a cookie.
Q 2 - 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 3 - 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 4 - 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)
Answer : B
Explaination
response.addIntHeader(name,date) adds a response header with the given name and integer value.
Q 5 - Which of the following code can be used to force any content in the buffer to be written to the client?
Answer : C
Explaination
response.flushBuffer() forces any content in the buffer to be written to the client.
Q 6 - Which of the following code can be used to clear any data that exists in the buffer as well as the status code and headers?
Answer : B
Explaination
response.reset() clears any data that exists in the buffer as well as the status code and headers.
Q 7 - Which of the following code can be used to clear the content of the underlying buffer in the response without clearing headers or status code.
Answer : C
Explaination
response.resetBuffer() clears the content of the underlying buffer in the response without clearing headers or status code.
Q 8 - 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 9 - Which of the following code can be used to send an error response to the client using the specified status code and error message?
A - request.sendError(statusCode,message)
B - response.sendError(statusCode,message)
Answer : B
Explaination
response.sendError(statusCode,message) sends an error response to the client using the specified status code and error message.
Q 10 - Which of the following code can be used to redirect user to different url?
A - request.sendRedirect(location)
B - response.sendRedirect(location)
Answer : B
Explaination
response.sendRedirect(location) sends a temporary redirect response to the client using the specified redirect location URL.
Q 11 - Which of the following code can be used to set the preferred buffer size for the body of the response?
A - response.setBufferSize(size)
B - request.setBufferSize(size)
Answer : A
Explaination
response.setBufferSize(size) sets the preferred buffer size for the body of the response.
Q 12 - 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)
Answer : A
Explaination
response.setCharacterEncoding(charset) sets the character encodinge for the body of the response.
Q 13 - 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 14 - Which of the following code can be used to set the content type for the body of the response?
A - request.setContentType(type)
B - response.setContentType(type)
Answer : B
Explaination
response.setContentType(type) sets the content type for the body of the response.
Q 15 - 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 16 - Which of the following code can be used to set the status code of the response?
A - response.setStatus(statusCode)
B - request..setStatus(statusCode)
Answer : A
Explaination
response.setLocale(locale) sets the status code of the response.
Q 17 - Which of the following is true about filters?
Answer : C
Explaination
Servlet Filters are Java classes that can be used to intercept requests from a client before they access a resource at back end and to manipulate responses from server before they are sent back to the client.
Q 18 - Which of the following are valid servlet filters?
Answer : D
Explaination
All of the above filters are suggested by the servlet specifications.
Q 19 - Which of the following is the correct order of filter life cycle phase methods?
A - init(), service(), destroy()
B - initialize(), service(), destroy()
Answer : A
Explaination
init() method is called by the web container to indicate to a filter that it is being placed into service. doFilter() method is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. destroy() method is called by the web container to indicate to a filter that it is being taken out of service.
Q 20 - When init() method of filter gets called?
B - The init() method is called whenever the servlet being filtered is invoked.
Answer : A
Explaination
The init method is designed to be called only once. It is called by the web container to indicate to a filter that it is being placed into service.
Q 21 - Which of the following is true about init() method of filter?
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 filter 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 filter.
Q 22 - When doFilter() method of servlet gets called?
A - The doFilter() method is called when the filter is first created.
B - The doFilter() method is called whenever the servlet being filtered is invoked.
Answer : B
Explaination
doFilter() method is called by the container each time a request/response pair is passed through the chain due to a client request for a resource/servlet at the end of the chain.
Q 23 - When destroy() method of filter gets called?
A - The destroy() method is called only once at the end of the life cycle of a filter.
B - The destroy() method is called after the filter has executed doFilter method.
Answer : A
Explaination
The destroy() method is called only once at the end of the life cycle of a filter.
Q 24 - 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 25 - 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.
Answer Sheet
Question Number | Answer Key |
---|---|
1 | B |
2 | A |
3 | B |
4 | B |
5 | C |
6 | B |
7 | C |
8 | B |
9 | B |
10 | B |
11 | A |
12 | A |
13 | B |
14 | B |
15 | B |
16 | A |
17 | C |
18 | D |
19 | A |
20 | A |
21 | C |
22 | B |
23 | A |
24 | A |
25 | D |