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.

Questions and Answers

Servlets Mock Test III

Answer : B

Explaination

response.addCookie(cookie) adds the specified cookie to the response and can be used to write a cookie.

Answer : A

Explaination

response.addDateHeader(name,date) adds a response header with the given name and date-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)

C - Header.addDateHeader(name,value)

D - None of the above.

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?

A - request.flushBuffer()

B - response.flush()

C - response.flushBuffer()

D - None of the above.

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?

A - request.reset()

B - response.reset()

C - response.resetBuffer()

D - None of the above.

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.

A - request.reset()

B - response.reset()

C - response.resetBuffer()

D - None of the above.

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)

C - header.sendError(statusCode)

D - None of the above.

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)

C - header.sendError(statusCode,message)

D - None of the above.

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)

C - header.sendRedirect(location)

D - None of the above.

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)

C - header.setBufferSize(size)

D - None of the above.

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)

C - header.setCharacterEncoding(charset)

D - None of the above.

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)

C - header.setContentLength(length)

D - None of the above.

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)

C - header.setContentType(type)

D - None of the above.

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?

A - request.setLocale(locale)

B - response.setLocale(locale)

C - header.setLocale(locale)

D - None of the above.

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)

C - header..setStatus(statusCode)

D - None of the above.

Answer : A

Explaination

response.setLocale(locale) sets the status code of the response.

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.

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()

C - init(), doFilter(), destroy()

D - init(), service(), delete()

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.

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.

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.

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.

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?

A - error-page

B - error-handler

C - exception

D - exception-handler

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

C - javax.servlet.error.message

D - All of the above.

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
servlets-questions-answers.htm
Advertisements